2011-01-30 49 views
5

一個我如何選擇輸入字段的值設置爲未禁用jQuery的 - 一個選擇的值設置爲它的選項

EG的第一選項的值,在這一領域,所選值爲1.如何將此值更改爲下一個非禁用選項,即。 2?

<select> 
    <option disabled="disabled" value="1">1</option 
    <option value="2">2</option 
    <option disabled="disabled" value="3">3</option 
    <option value="4">4</option 
</select> 

回答

12

爲您給出的例子,我會用:

$("select option:not([disabled])").first().attr("selected", "selected");

如果你選擇了一個特定的ID (例如「#foo」):

在選擇結束第一:

$("#foo option:not([disabled])").first().attr("selected", "selected");

1
$('#id-for-select option:not(:disabled):first').click() 
+0

會不會有成爲一個? – 2011-01-30 10:38:55

+0

嘿,是的。我對該效果的編輯與您的評論條目相交叉。 – chaos 2011-01-30 10:39:57

+0

無法正常工作.... – Alex 2011-01-30 10:45:52

0

設置選項的被選屬性:

<option id="value2" value="2">2</option> 

document.getElementById('value2').selected = true; 
1

試試這個:比其他解決方案更清潔

$('#select-id-here option:enabled').first().attr('selected', 'selected'); 
3

了一下:

$('#select_id').find('option:enabled:first').prop('selected',true); 
相關問題