gpt4 book ai didi

javascript - 带有搜索按钮的jquery下拉框

转载 作者:太空宇宙 更新时间:2023-11-04 12:33:29 25 4
gpt4 key购买 nike

我的元素要求我需要一个带搜索按钮的下拉框。我创建了一个,但它只是第一次工作。问题是如果找到搜索匹配,我将其他元素清空。所以下一次点击自其他元素是空的,无法正常工作。我应该如何解决这个问题,以便所有元素第一次出现,每次出现匹配项时只出现匹配的元素。请帮助我。我应该如何解决?我已经尝试了很多,这是我的代码。

html

<html>
<head>
<script src="jquery-1.11.0.js"></script>
</head>
<style>
.sel-box{
position:relative;
}
a{
text-decoration:none;
}
#select{
display:block;
width:235px;
height:20px;
border:1px solid #999;
padding:5px;
}
.toc-odd{
position:absolute;
top:32px;
background:#f1f1f1;
width:400px;
display:none;
}
.toc-odd li{
padding:5px 10px;
border-bottom:1px solid #999;
}
</style>
<body>
<p id ="demo"></p>

<div class="sel-box">
<span id='select'>Select</span>
<ul class='toc-odd level-1' id="sel-option">
</body>
</html>

脚本

$(function() {
$('#select').click(function() {
$('#sel-option').show();
var x = $("#sel-option .my_div").text();
$("#demo").html(x);

});

$('#sel-option a').click(function() {
$('#select').text($(this).text());
$('#sel-option').hide();
$(this).addClass('current');
e.preventDefault();
})
})


var employees = [{
"firstName": "John",
"lastName": "Doe"
}, {
"firstName": "Anna",
"lastName": "Smith"
}, {
"firstName": "Peter",
"lastName": "Jones"
}, ];
document.write("<div class=my_div>");
document.write("<li>" + "<input type=text id=searchTXT name=firstName >" + "<" + "button id=button1" + ">" + "Search " + "</button>");
document.write("</div>");
for (i = 0; i < employees.length; i++) {
document.write("<div class=" + employees[i].firstName + ">");
document.write("<li>" + "<a href=" + employees[i].firstName + ">" + employees[i].firstName + "</a>" + "</li>");
document.write("</div>");
}

document.write("</ul>");

$("#sel-option button").click(function() {

var x = $("#sel-option input").val();

for (i = 0; i < employees.length; i++) {
if (x != employees[i].firstName) {
$("." + employees[i].firstName).empty();
}

}

})

最佳答案

您必须重新创建下拉框,为此不要使用 document.write,但是..REDDIT

<p id ="demo"></p>
<div class="sel-box">
<span id='select'>Select</span>
<ul class='toc-odd level-1' id="sel-option"></ul>
<div class="my_div">
<input type=text id=searchTXT name=firstName/>
<button id=btnSearch>Search</button>
<div id="myDropDown"></div>
</div>
</div>

<script>
function createDropDown(){
htm = ""
for (i = 0; i < employees.length; i++) {
htm += "<div class=" + employees[i].firstName + ">";
htm += "<li>" + "<a href=" + employees[i].firstName + ">" + employees[i].firstName + "</a>" + "</li>";
htm += "</div>"
$("#myDropDown").html(htm)
}
}

createDropDown()

$("#btnSearch").click(function() {
console.log("click..")
createDropDown()
var x = $("input#searchTXT").val();
for (i = 0; i < employees.length; i++) {
if (x != employees[i].firstName) {
$("." + employees[i].firstName).empty();
console.log("empty " + x)
}

}

})


</script>

关于javascript - 带有搜索按钮的jquery下拉框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27459309/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com