
本教程旨在指导开发者如何利用javascript获取html输入框的值,并通过按钮事件触发数据筛选功能。文章详细介绍了document.getelementbyid().value的用法,以及如何将用户输入传递给javascript函数进行数据处理,从而实现动态、交互式的搜索体验,并强调了大小写转换在搜索中的重要性。
在现代网页应用中,用户输入是实现动态交互的关键。本教程将详细讲解如何通过JavaScript获取HTML表单输入框(input元素)的值,并结合按钮点击事件,对预定义的数据集进行筛选和展示。我们将以一个职位搜索功能为例,演示这一过程。
1. 准备HTML结构
首先,我们需要构建用户界面,包括两个文本输入框用于输入职位名称和地点,以及一个按钮用于触发搜索。同时,为了展示搜索结果,我们还需要一个容器元素。
动态职位搜索 body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; margin: 20px; background-color: #f4f7f6; color: #333; } h1 { color: #2c3e50; } form { background-color: #fff; padding: 20px; border-radius: 8px; box-shadow: 0 2px 5px rgba(0,0,0,0.1); margin-bottom: 20px; } input[type="text"] { padding: 10px; margin-right: 10px; border: 1px solid #ccc; border-radius: 4px; width: 200px; box-sizing: border-box; } button { padding: 10px 20px; background-color: #007bff; color: white; border: none; border-radius: 4px; cursor: pointer; font-size: 16px; transition: background-color 0.3s ease; } button:hover { background-color: #0056b3; } #results { margin-top: 20px; } .job-item { background-color: #e9ecef; border: 1px solid #dee2e6; padding: 15px; margin-bottom: 10px; border-radius: 6px; } .job-item strong { color: #0056b3; } .no-results { color: #dc3545; font-style: italic; } .summary { font-weight: bold; margin-top: 15px; color: #28a745; }职位搜索
请注意,我们将JavaScript代码放在一个单独的文件 script.js 中,并在HTML底部通过 引入,这是一种良好的实践,有助于分离结构和行为。
2. 定义数据源
在JavaScript中,我们需要一个包含职位信息的数组作为数据源。每个职位对象应包含 title(职位名称)和 location(地点)属性。
立即学习“Java免费学习笔记(深入)”;
Visual Studio IntelliCode
微软VS平台的 AI 辅助开发工具
46 查看详情
// script.jsconst jobs = [{ title: "Marketing Intern", location: "US, NY, New York" }, { title: "Customer Service - Cloud Video Production", location: "NZ, Auckland", }, { title: "Commissioning Machinery Assistant (CMA)", location: "US, IA, Wever", }, { title: "Account Executive - Washington DC", location: "US, DC, Washington", }, { title: "Bill Review Manager", location: "US, FL, Fort Worth" }, { title: "Accounting Clerk", location: "US, MD," }, { title: "Head of Content (m/f)", location: "DE, BE, Berlin" }, { title: "Lead Guest Service Specialist", location: "US, CA, San Francisco", }, { title: "HP BSM SME", location: "US, FL, Pensacola" }, { title: "Customer Service Associate - Part Time", location: "US, AZ, Phoenix", }, { title: "ASP.net Developer Job opportunity at United States,New Jersey", location: "US, NJ, Jersey City", }, { title: "Talent Sourcer (6 months fixed-term contract)", location: "GB, LND, London", }, { title: "Applications Developer, Digital", location: "US, CT, Stamford", }, { title: "Installers", location: "US, FL, Orlando" }, { title: "Account Executive - Sydney", location: "AU, NSW, Sydney" }, { title: "VP of Sales - Vault Dragon", location: "SG, 01, Singapore", }, { title: "Hands-On QA Leader", location: "IL, Tel Aviv, Israel" }, { title: "Southend-on-Sea Traineeships Under NAS 16-18 Year Olds Only", location: "GB, SOS, Southend-on-Sea", }, { title: "Visual Designer", location: "US, NY, New York" }, { title: "Process Controls Engineer - DCS PLC MS Office - PA", location: "US, PA, USA Northeast", }, { title: "Marketing Assistant", location: "US, TX, Austin" }, { title: "Front End Developer", location: "NZ, N, Auckland" }, { title: "Engagement Manager", location: "AE," }, { title: "Vice President, Sales and Sponsorship (Businessfriend.com)", location: "US, CA, Carlsbad", }, { title: "Customer Service", location: "GB, LND, London" }, { title: "H1B SPONSOR FOR L1/L2/OPT", location: "US, NY, New York" }, { title: "Marketing Exec", location: "SG," }, { title: "HAAD/DHA Licensed Doctors Opening in UAE", location: "AE, AZ, Abudhabi", }, { title: "Talent Management Process Manager", location: "US, MO, St. Louis", }, { title: "Customer Service Associate", location: "CA, ON, Toronto" }, { title: "Customer Service Technical Specialist", location: "US, MA, Waltham", }, { title: "Software Applications Specialist", location: "US, KS," }, { title: "Craftsman Associate", location: "US, WA, Everett" }, { title: "Completion Engineer", location: "US, CA, San Ramon" }, { title: "I Want To Work At Karmarama", location: "GB, LND," }, { title: "English Teacher Abroad", location: "US, NY, Saint Bonaventure", },];
3. 实现搜索逻辑
现在,我们将编写 searchAndDisplayJobs() 函数。这个函数将在用户点击“搜索”按钮时执行,负责获取输入值、筛选数据并更新页面。
3.1 获取用户输入
使用 document.getElementById(‘id’) 方法可以获取到HTML元素,然后通过 .value 属性可以获取该输入框的当前值。为了实现大小写不敏感的搜索,我们通常会将获取到的值转换为小写,即使用 .toLowerCase() 方法。
// script.js (接上文)function searchAndDisplayJobs() { // 获取职位名称输入框的值并转换为小写 const titleQuery = document.getElementById('title').value.toLowerCase(); // 获取地点输入框的值并转换为小写 const locationQuery = document.getElementById('locationInput').value.toLowerCase(); // 获取用于显示结果的DOM元素 const resultsDiv = document.getElementById('results'); // 在每次搜索前清空上次的结果,避免重复显示 resultsDiv.innerHTML = ''; let count = 0; // 记录找到的职位数量 const matchingJobs = []; // 存储匹配的职位 // 遍历所有职位数据进行筛选 jobs.forEach(job => { const lowercaseTitle = job.title.toLowerCase(); const lowercaseLocation = job.location.toLowerCase(); // 判断职位标题和地点是否包含用户输入 if (lowercaseTitle.includes(titleQuery) && lowercaseLocation.includes(locationQuery)) { matchingJobs.push(job); count++; } }); // 根据筛选结果更新页面 if (matchingJobs.length > 0) { matchingJobs.forEach(job => { const jobElement = document.createElement('div'); jobElement.className = 'job-item'; // 添加样式类 jobElement.innerHTML = ` 职位: ${job.title}
地点: ${job.location}
`; resultsDiv.appendChild(jobElement); }); } else { resultsDiv.innerHTML = '未找到匹配的职位。
'; } // 显示搜索结果摘要 const summaryElement = document.createElement('p'); summaryElement.className = 'summary'; summaryElement.textContent = `共找到 ${count} 个匹配职位。`; resultsDiv.appendChild(summaryElement);}
3.2 完整示例代码
将上述HTML和JavaScript代码结合起来,即可实现完整的动态职位搜索功能。
动态职位搜索 body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; margin: 20px; background-color: #f4f7f6; color: #333; } h1 { color: #2c3e50; } form { background-color: #fff; padding: 20px; border-radius: 8px; box-shadow: 0 2px 5px rgba(0,0,0,0.1); margin-bottom: 20px; } input[type="text"] { padding: 10px; margin-right: 10px; border: 1px solid #ccc; border-radius: 4px; width: 200px; box-sizing: border-box; } button { padding: 10px 20px; background-color: #007bff; color: white; border: none; border-radius: 4px; cursor: pointer; font-size: 16px; transition: background-color 0.3s ease; } button:hover { background-color: #0056b3; } #results { margin-top: 20px; } .job-item { background-color: #e9ecef; border: 1px solid #dee2e6; padding: 15px
以上就是JavaScript与HTML输入交互:实现动态数据筛选的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/875989.html
微信扫一扫
支付宝扫一扫