
在现代web应用中,提供高效且用户友好的搜索功能至关重要。这通常涉及到从用户界面获取输入,动态构建包含查询参数的url,并在新标签页中打开这些链接以展示搜索结果。本教程将引导您完成一个实际案例,演示如何利用javascript实现这一功能,包括处理用户选择(如公司或个人搜索)、多省份/地区选择,以及如何应对浏览器对弹出窗口的拦截。
实现上述功能主要依赖以下JavaScript和Web API:
为了实现搜索功能,我们需要以下关键HTML元素:
以下是相关HTML片段:
<div id="company-search-field"> <input id="company-name" name="company-name" placeholder="Insert company name here" /> </div> <div id="person-search-field" style="display: none;"> <input id="first-name" name="first-name" placeholder="Insert person first name here" /> <input id="last-name" name="last-name" placeholder="Insert person last name here" /> </div> <div class="search-subject"> <input type="radio" onclick="displayPersonOrCompanySearchFields()" id="company-subject" name="subject-type" value="company-subject" checked> <label for="company-subject">Company</label> <input type="radio" onclick="displayPersonOrCompanySearchFields()" id="person-subject" name="subject-type" value="person-subject"> <label for="person-subject">Person</label> </div> <!-- WHEN COMPANY IS SELECTED --> <div id="company-search-states"> <!-- ... 省份/地区复选框列表 ... --> <button onclick="openCorporateRecordTabs();">Search Corporate Records</button> <button onclick="openSelectedStateTabs();">Search Litigation Cases</button> </div> <!-- WHEN PERSON IS SELECTED --> <div id="person-search-states" style="display: none;"> <!-- ... 省份/地区复选框列表 ... --> <button onclick="openCorporateRecordTabs();">Search Corporate Records</button> <button onclick="openSelectedStateTabs();">Search Litigation Cases</button> </div>
此函数根据用户选择的搜索类型(公司或个人)来显示或隐藏相应的输入字段和省份选择区域。
立即学习“Java免费学习笔记(深入)”;
function displayPersonOrCompanySearchFields() {
let person = document.getElementById("person-subject");
let company = document.getElementById("company-subject");
let person_states = document.getElementById("person-search-states");
let company_states = document.getElementById("company-search-states");
let person_field = document.getElementById("person-search-field");
let company_field = document.getElementById("company-search-field");
if (person.checked === true) {
person_states.style.display = "block";
person_field.style.display = "block";
company_states.style.display = "none";
company_field.style.display = "none";
} else {
company_states.style.display = "block";
company_field.style.display = "block";
person_states.style.display = "none";
person_field.style.display = "none";
}
}这是一个JavaScript对象,存储了不同省份/地区对应的基础搜索URL。某些地区可能对应多个URL。
const corporaterecordsUrls = {
"federal-level": ["https://www.ic.gc.ca/app/scr/cc/CorporationsCanada/fdrlCrpSrch.html?locale=en_CA"],
"alberta": ["https://beta.canadasbusinessregistries.ca/search"],
"british-columbia": ["https://beta.canadasbusinessregistries.ca/search", "https://www.bccourts.ca/search_judgments.aspx", "https://www.provincialcourt.bc.ca/judgments.php?link=https://www.canlii.org/en/bc/bcpc/"],
// ... 其他省份/地区URL ...
"yukon": ["https://ycor-reey.gov.yk.ca/search"],
};这是核心功能函数,它负责:
const openCorporateRecordTabs = () => {
// 获取所有被选中的省份/地区复选框
const selectedStateCheckboxes = document.querySelectorAll('input[name="state-name"]:checked');
// 判断当前是个人搜索还是公司搜索
const isPersonSearch = document.getElementById("person-subject").checked;
let searchTerm = '';
if (isPersonSearch) {
// 如果是个人搜索,获取姓和名并组合
const firstName = document.getElementById("first-name").value.trim();
const lastName = document.getElementById("last-name").value.trim();
if (firstName || lastName) {
searchTerm = (firstName + ' ' + lastName).trim();
}
} else {
// 如果是公司搜索,获取公司名
searchTerm = document.getElementById("company-name").value.trim();
}
// 检查是否选择了省份/地区,并且个人搜索时是否有输入
if (selectedStateCheckboxes.length === 0) {
alert("请选择至少一个省份或地区。");
return;
}
if (isPersonSearch && !searchTerm) {
alert("请在个人搜索模式下输入姓名。");
return;
}
if (!isPersonSearch && !searchTerm) {
alert("请在公司搜索模式下输入公司名称。");
return;
}
// 映射选中的省份到它们对应的URL,并根据需要添加搜索查询参数
const urlsToOpen = Array.from(selectedStateCheckboxes).flatMap(checkbox => {
const stateValue = checkbox.value;
const baseUrls = corporaterecordsUrls[stateValue]; // 获取该省份对应的所有基础URL
if (!baseUrls) {
console.warn(`未找到州/省份: ${stateValue} 的URL配置。`);
return []; // 如果没有定义URL,则跳过
}
return baseUrls.map(baseUrl => {
// 特殊处理:如果当前是个人搜索,且URL是针对Yukon的搜索页面,则动态添加姓名参数
if (isPersonSearch && stateValue === "yukon" && searchTerm) {
// 假设Yukon的基础URL是 https://ycor-reey.gov.yk.ca/search
if (baseUrl.startsWith("https://ycor-reey.gov.yk.ca/search")) {
const url = new URL(baseUrl);
url.searchParams.set('name', searchTerm); // 添加姓名参数
if (!url.searchParams.has('reference')) {
url.searchParams.set('reference', ''); // 根据需求添加reference参数
}
return url.toString(); // 返回包含查询参数的完整URL
}
}
// 对于其他情况(公司搜索或非特定个人搜索URL),直接使用基础URL
return baseUrl;
});
});
// 遍历并打开所有生成的URL
urlsToOpen.forEach(url => {
const win = window.open(url, '_blank'); // '_blank' 确保在新标签页中打开
if (win) {
// 浏览器允许打开弹出窗口
win.focus(); // 聚焦到新打开的标签页
} else {
// 浏览器拦截了弹出窗口
alert(`尝试打开 ${url} - 您的浏览器阻止了弹出窗口。请允许此网站的弹出窗口。`);
}
});
};虽然问题答案中没有提供 openSelectedStateTabs 的具体实现,但其在HTML中被调用,并且功能与 openCorporateRecordTabs 类似,可能只是打开不同类型的链接或进行不同的查询。如果需要,可以按照 openCorporateRecordTabs 的模式进行扩展。
// 假设 openSelectedStateTabs 函数也需要类似的处理逻辑
// 这里仅提供一个骨架,具体实现取决于“Search Litigation Cases”的业务需求
const openSelectedStateTabs = () => {
const selectedStateCheckboxes = document.querySelectorAll('input[name="state-name"]:checked');
const isPersonSearch = document.getElementById("person-subject").checked;
let searchTerm = '';
if (isPersonSearch) {
const firstName = document.getElementById("first-name").value.trim();
const lastName = document.getElementById("last-name").value.trim();
if (firstName || lastName) {
searchTerm = (firstName + ' ' + lastName).trim();
}
} else {
searchTerm = document.getElementById("company-name").value.trim();
}
if (selectedStateCheckboxes.length === 0) {
alert("请选择至少一个省份或地区。");
return;
}
// ... 进一步的输入校验 ...
const litigationUrlsToOpen = Array.from(selectedStateCheckboxes).flatMap(checkbox => {
const stateValue = checkbox.value;
// 假设存在一个 `litigationUrls` 对象或在 `corporaterecordsUrls` 中有特定标识的诉讼URL
// 例如:
// const litigationBaseUrls = litigationUrls[stateValue];
// 或者从 corporaterecordsUrls 中筛选出诉讼相关的URL
const corporateBaseUrls = corporaterecordsUrls[stateValue];
// 这里需要根据实际需求,从 corporateBaseUrls 中选择或构造诉讼相关的URL
// 示例:如果 British Columbia 的第二个URL是诉讼相关的
return corporateBaseUrls.filter(url => url.includes("bccourts.ca")); // 仅为示例
// 同样,如果需要,可以像 openCorporateRecordTabs 那样添加动态查询参数
});
litigationUrlsToOpen.forEach(url => {
const win = window.open(url, '_blank');
if (win) {
win.focus();
} else {
alert(`尝试打开 ${url} - 您的浏览器阻止了弹出窗口。请允许此网站的弹出窗口。`);
}
});
};以上就是如何在JavaScript中实现动态搜索查询与多标签页打开功能的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号