JS中的Select框实现模糊搜索功能
您可以使用JavaScript来实现模糊搜索的功能,以下是一个简单的示例代码:HTML部分:
<select id="select">
<option value="1">Apple</option>
<option value="2">Banana</option>
<option value="3">Cherry</option>
<option value="4">Orange</option>
</select>JavaScript部分:
document.getElementById('select').addEventListener('input', function() {
var input, filter, option, i;
input = this.value;
filter = input.toUpperCase();
options = this.getElementsByTagName('option');
for (i = 0; i < options.length; i++) {
if (options.innerHTML.toUpperCase().indexOf(filter) > -1) {
options.style.display = '';
} else {
options.style.display = 'none';
}
}
});以上代码会监听select框的input事件,当用户输入内容时,会根据用户输入的内容来过滤选项。只有当某个选项的内容包含了用户输入的内容时,该选项才会显示出来,否则会被隐藏。这样就实现了模糊搜索的功能。
扩展:js实现模糊查询
今天为大家分享的是使用js实现模糊查询:
首先写出html
<div class="search">
<input type="text" class="inp">
<div class="but">搜索</div>
</div>
<div class="list"></div>再写css
* {
padding: 0;
margin: 0;
}
.search {
width: 96%;
margin-left: 2%;
height: 60px;
display: flex;
align-items: center;
justify-content: center;
}
.search input {
width: 60%;
height: 20px;
border: 1px #eeeeee solid;
padding: 5px;
border-top-left-radius: 20px;
border-bottom-left-radius: 20px;
}
.search div {
width: 20%;
height: 32px;
text-align: center;
line-height: 32px;
background-color: #eeeeee;
border-top-right-radius: 20px;
border-bottom-right-radius: 20px;
}
.list {
width: 96%;
margin-left: 2%;
}
.list div {
width: 100%;
height: 40px;
display: flex;
justify-content: space-around;
}
.list span {
display: block;
width: 20%;
line-height: 40px;
text-align: center;
}现在结构样式都有的就该写数据了
let arr = [{
name: "吴小糖",
addres: "河南",
base: 410725,
num: 9999
}, {
name: "吴三",
addres: "浙江",
base: 102419,
num: 10000
}, {
name: "吴磊",
addres: "北京",
base: 314345,
num: 5555
}, {
name: "吴谨言",
addres: "湖南",
base: 341026,
num: 7888
}, {
name: "吴爽",
addres: "河南",
base: 410756,
num: 6666
}, {
name: "吴宣仪",
addres: "海南",
base: 243187,
num: 9999
}]https://img.jbzj.com/file_images/article/202406/2024060611400226.png
下一步就该js部分了
let but = document.getElementsByClassName("but");
let inp = document.getElementsByClassName("inp");
sorts(arr);
but.onclick = function() {
let data = [];
for (let i = 0; i < arr.length; i++) {
for (let k in arr) {
if (String(arr).indexOf(inp.value) > -1) {
data.push(arr);
break;
}
}
}
sorts(data);
}
function sorts(a) {
let str = "";
for (let i = 0; i < a.length; i++) {
str += `<div>
<span>${a.name}</span>
<span>${a.base}</span>
<span>${a.num}</span>
<span>${a.addres}</span>
</div>`
}
document.getElementsByClassName("list").innerHTML = str;
}这样就制作完成了,整体代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
<style>
* {
padding: 0;
margin: 0;
}
.search {
width: 96%;
margin-left: 2%;
height: 60px;
display: flex;
align-items: center;
justify-content: center;
}
.search input {
width: 60%;
height: 20px;
border: 1px #eeeeee solid;
padding: 5px;
border-top-left-radius: 20px;
border-bottom-left-radius: 20px;
}
.search div {
width: 20%;
height: 32px;
text-align: center;
line-height: 32px;
background-color: #eeeeee;
border-top-right-radius: 20px;
border-bottom-right-radius: 20px;
}
.list {
width: 96%;
margin-left: 2%;
}
.list div {
width: 100%;
height: 40px;
display: flex;
justify-content: space-around;
}
.list span {
display: block;
width: 20%;
line-height: 40px;
text-align: center;
}
</style>
</head>
<body>
<div class="search">
<input type="text" class="inp">
<div class="but">搜索</div>
</div>
<div class="list"></div>
<script type="text/javascript">
let arr = [{
name: "吴小糖",
addres: "河南",
base: 410725,
num: 9999
}, {
name: "吴三",
addres: "浙江",
base: 102419,
num: 10000
}, {
name: "吴磊",
addres: "北京",
base: 314345,
num: 5555
}, {
name: "吴谨言",
addres: "湖南",
base: 341026,
num: 7888
}, {
name: "吴爽",
addres: "河南",
base: 410756,
num: 6666
}, {
name: "吴宣仪",
addres: "海南",
base: 243187,
num: 9999
}]
let but = document.getElementsByClassName("but");
let inp = document.getElementsByClassName("inp");
sorts(arr);
but.onclick = function() {
let data = [];
for (let i = 0; i < arr.length; i++) {
for (let k in arr) {
if (String(arr).indexOf(inp.value) > -1) {
data.push(arr);
break;
}
}
}
sorts(data);
}
function sorts(a) {
let str = "";
for (let i = 0; i < a.length; i++) {
str += `<div>
<span>${a.name}</span>
<span>${a.base}</span>
<span>${a.num}</span>
<span>${a.addres}</span>
</div>`
}
document.getElementsByClassName("list").innerHTML = str;
}
</script>
</body>
</html>
</body>
</html>以上就是本章的全部内容,感谢您的阅读。
到此这篇关于JS实现select框实现模糊搜索功能的文章就介绍到这了,更多相关js模糊搜索内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
来源:https://www.jb51.net/javascript/322328ev2.htm
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页:
[1]