gpt4 book ai didi

jquery - 通过输入框和 jquery 过滤选择列表

转载 作者:行者123 更新时间:2023-12-01 03:28:34 25 4
gpt4 key购买 nike

我想知道是否可以获得一些帮助,通过 jquery 使用输入框过滤选择列表。

这是我的 js 的样子,但它似乎不起作用。我猜这是因为选择列表中的选项不可隐藏。

    <script type="text/javascript">
$(document).ready(function() {
$("#inputFilter").change(function() {
var filter = $(this).val();

$("#selectList option").each(function() {
var match = $(this).text().search(new RegExp(filter, "i"));
if (match > 0) {
$(this).show(); // Does not work
}
else
$(this).hide();
});
});
});
</script>

这是我的 html

<input id="inputFilter" />
<select id="selectList">
<option value="1111" >1111 - London</option>
<option value="1112" >1112 - Paris </option>
</select>

最佳答案

请尝试这个:

$("#inputFilter").change(function() {
var filter = $(this).val();
//alert(filter);
$("#selectList option").each(function() {
var match = $(this).text().search(new RegExp(filter, "i"));
//alert(match);
if (match < 0 && $(this).text() != "--select--") {
$(this).attr("disabled",true);
}
else
$(this).attr("disabled",false);

});
});

您可以看到它的实际效果 here .

HTH

关于jquery - 通过输入框和 jquery 过滤选择列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2700356/

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