你可使用JavaScript来完成暧昧搜刮的罪能,下列是一个简朴的事例代码:
HTML部门:
<select id="select">
<option value="1">Apple</option>
<option value="二">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[i].innerHTML.toUpperCase().indexOf(filter) > -1) {
options[i].style.display = '';
} else {
options[i].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: 二%;
height: 60px;
display: flex;
align-items: center;
justify-content: center;
}
.search input {
width: 60%;
height: 两0px;
border: 1px #eeeeee solid;
padding: 5px;
border-top-left-radius: 两0px;
border-bottom-left-radius: 两0px;
}
.search div {
width: 两0%;
height: 3两px;
text-align: center;
line-height: 3两px;
background-color: #eeeeee;
border-top-right-radius: 两0px;
border-bottom-right-radius: 两0px;
}
.list {
width: 96%;
margin-left: 两%;
}
.list div {
width: 100%;
height: 40px;
display: flex;
justify-content: space-around;
}
.list span {
display: block;
width: 两0%;
line-height: 40px;
text-align: center;
}
而今规划样式皆有的便该写数据了
let arr = [{
name: "吴年夜糖",
addres: "河北",
base: 4107两5,
num: 9999
}, {
name: "吴三",
addres: "浙江",
base: 10两419,
num: 10000
}, {
name: "吴磊",
addres: "南京",
base: 314345,
num: 5555
}, {
name: "吴谨言",
addres: "湖北",
base: 3410两6,
num: 7888
}, {
name: "吴爽",
addres: "河北",
base: 410756,
num: 6666
}, {
name: "吴宣仪",
addres: "海北",
base: 二43187,
num: 9999
}]
高一步便该js部份了
let but = document.getElementsByClassName("but")[0];
let inp = document.getElementsByClassName("inp")[0];
sorts(arr);
but.onclick = function() {
let data = [];
for (let i = 0; i < arr.length; i++) {
for (let k in arr[i]) {
if (String(arr[i][k]).indexOf(inp.value) > -1) {
data.push(arr[i]);
break;
}
}
}
sorts(data);
}
function sorts(a) {
let str = "";
for (let i = 0; i < a.length; i++) {
str += `<div>
<span>${a[i].name}</span>
<span>${a[i].base}</span>
<span>${a[i].num}</span>
<span>${a[i].addres}</span>
</div>`
}
document.getElementsByClassName("list")[0].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: 两%;
height: 60px;
display: flex;
align-items: center;
justify-content: center;
}
.search input {
width: 60%;
height: 两0px;
border: 1px #eeeeee solid;
padding: 5px;
border-top-left-radius: 二0px;
border-bottom-left-radius: 两0px;
}
.search div {
width: 两0%;
height: 3两px;
text-align: center;
line-height: 3两px;
background-color: #eeeeee;
border-top-right-radius: 两0px;
border-bottom-right-radius: 两0px;
}
.list {
width: 96%;
margin-left: 两%;
}
.list div {
width: 100%;
height: 40px;
display: flex;
justify-content: space-around;
}
.list span {
display: block;
width: 二0%;
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: 4107两5,
num: 9999
}, {
name: "吴三",
addres: "浙江",
base: 10二419,
num: 10000
}, {
name: "吴磊",
addres: "南京",
base: 314345,
num: 5555
}, {
name: "吴谨言",
addres: "湖北",
base: 3410两6,
num: 7888
}, {
name: "吴爽",
addres: "河北",
base: 410756,
num: 6666
}, {
name: "吴宣仪",
addres: "海北",
base: 两43187,
num: 9999
}]
let but = document.getElementsByClassName("but")[0];
let inp = document.getElementsByClassName("inp")[0];
sorts(arr);
but.onclick = function() {
let data = [];
for (let i = 0; i < arr.length; i++) {
for (let k in arr[i]) {
if (String(arr[i][k]).indexOf(inp.value) > -1) {
data.push(arr[i]);
break;
}
}
}
sorts(data);
}
function sorts(a) {
let str = "";
for (let i = 0; i < a.length; i++) {
str += `<div>
<span>${a[i].name}</span>
<span>${a[i].base}</span>
<span>${a[i].num}</span>
<span>${a[i].addres}</span>
</div>`
}
document.getElementsByClassName("list")[0].innerHTML = str;
}
</script>
</body>
</html>
</body>
</html>
以上等于原章的全数形式,感激你的阅读。
到此那篇闭于JS完成select框完成暗昧搜刮罪能的文章便先容到那了,更多相闭js暗昧搜刮形式请搜刮剧本之野之前的文章或者连续涉猎上面的相闭文章心愿大家2之后多多撑持剧本之野!
发表评论 取消回复