2012-02-28 29 views
0

所有, 我用下面的代碼刪除使用jQuery一些選擇選項後:確定多少選擇元素上移除形式一個

$('#other_liked_vendors option[value=' + clicked_id + ']').remove(); 
length = $("#other_liked_vendors").val().length; 
alert(length); 
if($("#other_liked_vendors").val().length === 0){ 
    $("#view_other_liked_vendors").hide(); 
} 

基本上,我想說,如果最後選擇的元素已被刪除,那麼我想隱藏選擇元素的div。任何人都可以告訴我如何正確地檢查這個?

謝謝!

回答

1

您可以使用jQuery對象的.length屬性來查看匹配的元素數量。所以:

if ($('#other_liked_vendors option').length == 0) { 
    // all options have been removed 
} 

你試圖使用.val()的長度,這是當前所選選項的值的字符串的長度...

1

$("#other_liked_vendors").val().length會給你選擇的元素length選定的值。

你應該使用這個,它會在select元素中找到option元素並檢查它的length屬性。

if ($('#other_liked_vendors option').length == 0) { 
    $("#view_other_liked_vendors").hide(); 
} 
0

你可以這樣做:

 

if($('#other_liked_vendors option').size() == 0) { 
...... 
 

之一或更好

 

if($('#other_liked_vendors option').length == 0) { 
...... 
 
+1

大小()即將淘汰的JQuery,更好地堅持長 – charlietfl 2012-02-28 04:42:33

相關問題