2014-01-21 127 views
0

我有许多选项的Dorpdownlist。所以我想用文本过滤这些选项。从下拉列表中更新选项

$("input.form-control").change(function() { 
    var value = $('.form-control').val(); 

    //now I need to remove the options witch not contains the value in the text. 

}); 


<select id="id_customer" name="customer"> 
    <option value="1">Max Musterman </option> 
    <option value="2">Heidi Mustermann </option> 
</select> 

如果值发生变化,也许删除的选项必须再次添加。 这只是我第一步与jquery。如果你有Django,Ajax或其他的解决方案,那也没关系。

回答

0

您可以使用.filter()

$("input.form-control").change(function() { 
    var value = $('.form-control').val(); 

    $('#id_customer option').filter(function(){ 
     return $(this).text() != value; 
    }).remove(); 

}); 
+0

可能是最好的获得来自'option'的文字去掉后面的空格,因为他们将打破文本比较。 –

+0

有没有“含有”的方法?所以我必须输入洞名找到一个选项。如何再次显示删除的选项? – spitzbuaamy

+0

@spitzbuaamy你可以使用'indexOf'来检查它是否包含一些字符串。 –

相关问题