新闻中心
J*aScript与HTML输入交互:实现动态数据筛选

本教程旨在指导开发者如何利用j*ascript获取html输入框的值,并通过按钮事件触发数据筛选功能。文章详细介绍了document.getelementbyid().value的用法,以及如何将用户输入传递给j*ascript函数进行数据处理,从而实现动态、交互式的搜索体验,并强调了大小写转换在搜索中的重要性。
在现代网页应用中,用户输入是实现动态交互的关键。本教程将详细讲解如何通过J*aScript获取HTML表单输入框(input元素)的值,并结合按钮点击事件,对预定义的数据集进行筛选和展示。我们将以一个职位搜索功能为例,演示这一过程。
1. 准备HTML结构
首先,我们需要构建用户界面,包括两个文本输入框用于输入职位名称和地点,以及一个按钮用于触发搜索。同时,为了展示搜索结果,我们还需要一个容器元素。
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>动态职位搜索</title>
<style>
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; }
</style>
</head>
<body>
<h1>职位搜索</h1>
<form>
<input type="text" id="title" placeholder="请输入职位名称">
<input type="text" id="locationInput" placeholder="请输入地点">
<button type="button" onclick="searchAndDisplayJobs()">搜索</button>
</form>
<div id="results"></div>
<script src="script.js"></script>
</body>
</html>请注意,我们将J*aScript代码放在一个单独的文件 script.js 中,并在HTML底部通过 引入,这是一种良好的实践,有助于分离结构和行为。
2. 定义数据源
在J*aScript中,我们需要一个包含职位信息的数组作为数据源。每个职位对象应包含 title(职位名称)和 location(地点)属性。
立即学习“J*a免费学习笔记(深入)”;
// script.js
const 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 Bon*enture",
},
];3. 实现搜索逻辑
现在,我们将编写 searchAndDisplayJobs() 函数。这个函数将在用户点击“搜索”按钮时执行,负责获取输入值、筛选数据并更新页面。
Kreado AI
Kreado AI是一个多语言AI视频创作平台,只需输入文本或关键词,即可创作真实/虚拟人物的多语言口播视频。 为创作者提供AI赋能
182
查看详情
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 = `
<p><strong>职位:</strong> ${job.title}</p>
<p><strong>地点:</strong> ${job.location}</p>
`;
resultsDiv.appendChild(jobElement);
});
} else {
resultsDiv.innerHTML = '<p class="no-results">未找到匹配的职位。</p>';
}
// 显示搜索结果摘要
const summaryElement = document.createElement('p');
summaryElement.className = 'summary';
summaryElement.textContent = `共找到 ${count} 个匹配职位。`;
resultsDiv.appendChild(summaryElement);
}3.2 完整示例代码
将上述HTML和J*aScript代码结合起来,即可实现完整的动态职位搜索功能。
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>动态职位搜索</title>
<style>
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以上就是J*aScript与HTML输入交互:实现动态数据筛选的详细内容,更多请关注其它相关文章!
# 肇庆网站建设优化
# 请输入
# 搜索结果
# 如何处理
# 搜索功能
# 多语言
# 是一个
# 烟台正宗网站建设费用多少
# 苏州产品关键词排名
# 转换为
# 网站建站和优化
# seo修复
# 密云SEO
# 衡阳seo公司信誉保障
# 怎么找合伙人网站推广员
# 宜昌网站推广服务
# 怎么通过网站推广
# javascript
# 如何实现
# 输入框
# 关键词
# ht
# nas
# ai
# mac
# office
# oppo
# app
# go
# git
# js
# html
# java
相关栏目:
【
科技资讯46185 】
【
网络学院92790 】
相关推荐:
PS5 Pro有点优势但不多! 《燕云十六声》PS5平台与PC性能画面对比
漫蛙官网正版漫画入口 漫蛙2官方网页登录地址
Tabulator表格日期时间排序问题及自定义解决方案
神庙逃亡小游戏在线玩 神庙逃亡小游戏入口
zookeeper 都有哪些功能?
如何使用纯J*aScript判断Input元素是否在特定类容器内
WordPress插件开发:正确注册卸载钩子与避免常见陷阱
PHP表单数据传递:如何通过隐藏输入字段获取动态ID
TypeScript/J*aScript:高效查找数组中首个唯一ID对象
利用5118提升短视频内容效果_5118短视频关键词优化方法
fishbowl官网免费版 fishbowl养鱼网站入口
海棠账号登录入口_登录海棠账户同步阅读记录
一加手机电池耗电快怎么办_一加手机电池耗电快的解决方法
Yandex免登录官网入口_俄罗斯Yandex搜索引擎直达链接
Adobe PDF表单中利用J*aScript解析与格式化日期组件的教程
如何在CSS中使用浮动制作导航栏_float实现水平菜单
漫蛙MANWA漫画主页官方入口 漫蛙漫画最新在线阅读地址
火锅吃太多会怎样 火锅吃太多会上火吗
支付宝如何管理隐私设置_支付宝隐私保护的配置技巧
word中如何让数字纵向排列_Word数字纵向排列方法
不同用户不同价格! 索尼开启账户个性化定价测试
Python模块化编程:有效管理依赖与避免循环引用
Linux如何排查内存不足OOME问题_LinuxOOM分析教程
Sublime Text怎么设置垂直标尺_Sublime配置Rulers规范代码长度
怎么去除衣服上的口红印_生活小妙招教你用酒精轻松擦除
c++如何实现一个简单的软件渲染器_c++从零开始的3D图形学
steam官方网页快速访问 steam账号注册全流程
如何使用J*aScript精确选择并批量修改特定父元素下子链接的样式
顺丰快递查单号物流信息 顺丰快递小程序查询入口
智慧团建扫码登录入口 智慧团建扫码登录入口官网版
Yandex免登录网页版地址 Yandex搜索引擎官方访问入口
服务端验证_j*ascript输入检查
192.168.1.1管理中心入口 192.168.1.1路由器网页设置平台
网易大神怎么保存别人动态的图片_网易大神动态图片保存方法
excel怎么制作工资条 excel快速生成工资条的方法
Golang如何使用buffered channel提高性能_Golang buffered channel优化技巧
Safari怎么安装扩展程序 浏览器插件安装与管理方法【详解】
Excel Power Pivot如何处理XML数据源 构建高级数据模型
Win11截图该按哪些键 Win11截屏完整流程解析【教程】
解决 Vaadin 8 中大文件音频播放与定位时出现的 IOException
如何在Python中使用Optional类型处理可变对象并避免Pylint警告
消息称三星明年 2 月正式发布 HBM4,与 SK 海力士同台竞技
夸克AO3官网入口_AO3镜像网站2025推荐
html5 app怎么运行环境_配html5 app运行环境【教程】
腾讯QQ邮箱官方网站_QQ邮箱网页版在线登录
高德地图总提示网络异常怎么办 高德地图离线导航设置与网络排查方法
J*aScript DOM操作:高效清空列表元素的策略与实践
Win11怎么设置鼠标指针速度_Win11提高鼠标指针精确度选项
响应式图片在网页设计中的正确实现方法
React Router v6 教程:构建认证保护的私有路由与重定向策略


2025-11-11
浏览次数:次
返回列表
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 Bon*enture",
},
];