2016-08-22 63 views
1

如何獲取所有沒有使用jQuery選擇選項的選擇元素?如何獲取所有沒有使用jQuery選擇的選項的選擇元素?

<select id="one"> 
    <option value=""></option> 
    <option value="test"></option> 
</select> 

<select id="two"> 
    <option value=""></option> 
    <option selected value="test"></option> 
</select> 

jQuery選擇器會根據沒有選擇返回#one嗎?

+0

''元素選擇了你的第一個選項(''「'' ? – yuriy636

+0

是的,這聽起來是正確的,但「」默認情況下沒有選定的屬性。 –

回答

3
$('select option:selected[value=""]').parent() 
  • 選擇所有所有select元素
  • 檢查所選擇的選項的""一個值,而你的情況意味着沒有選項實際選擇的:selected選項。
  • 返回父(這將是一個select
+2

這不會產生與提問者所要求的相反嗎?這給所有'選擇'元素*做*有一個選擇的選項.. – EyasSH

+0

哦,是的,謝謝。 – yuriy636

+0

作品完美,謝謝! –

0

您可以利用jQuery的.parent().not()函數。請看下圖:

// selector for all 'select' elements with any option below it 
var all = $("select>option").parent(); // alternative $("select") 

// selector for all 'select' element with a selected child 
var selected = $("select>option[selected]").parent(); 

// the subtraction set "all - selected" achieved by `not`. 
var unselected = all.not(selected); 

注意,jQuery的parent負責刪除重複的從一組子元素的父母。

JsFiddle here

0

如果您有jQuery庫比試圖 $( '選擇選項')。過濾器(功能(I,d){回報!d.hasAttribute ( 「選擇」)});

相關問題