2015-04-28 49 views
0

使用jquery如何在1語句中通過其值或文本選擇一個選項?jQuery通過文本或值的選擇選項

<select> 
    <option value="0">One</option> 
    <option value="1">Two</option> 
</select> 

我可以單獨做這兩條語句,但是如何將以下兩條語句合併到一條select OR語句中呢?

$('select option[text="Two"]'); //This selects by text 

$('select option[value="4"]'); //This selects by value 

回答

0

[text="1"]不適用於jquery版本1.9+。您需要使用.filter()過濾掉元素具有文本Two條件:

$('select option[value="Two"]').filter(function() { 
     return $(this).text() == "Two"; 
}).val(); 
0

您可以使用jQuery :contains方法,並選擇該選項的文本。 Demo

alert($('select option[value="0"]').text()); 
alert($('select option:contains(One)').val());