2014-01-21 173 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'來檢查它是否包含一些字符串。 –

相關問題