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

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

这是我的js看起来像,但它似乎没有工作。 我猜这是因为选择列表中的选项不可隐藏。

 $(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(); }); }); });  

这是我的HTML

   1111 - London 1112 - Paris   

请试试这个:

 $("#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); }); }); 

你可以在这里看到它。

HTH

尝试禁用而不是隐藏。

 $(this).attr('disabled', 'disabled'); 

您可以做的另一件事是完全删除DOM中的选项。

没有text属性。

尝试这样的事情:

    

编辑:编辑我的答案,因为我误读了一些事情。