2013-10-18 27 views
0

目前我的代碼看起來像這樣,的jQuery的onchange選擇選項不工作

$("#select").change(function() { 
    var thes = $(this).attr('value'); 
    var next = $('option[:selected]').next().attr('value'); 
    alert(next); 
}); 

和我select看起來像這樣

<select id="select"> 
    <option value="1">1</option> 
    <option value="2">2</option> 
    <option value="3">3</option> 
</select> 

我使用alert變量thes審判,它會彈出但是當我使用變量next它不會。我的代碼有什麼問題?請幫忙。

+1

'.attr( '值');'可以用'.VAL)來代替(' – Praveen

回答

4

變化

'option[:selected]' 

'option:selected' 

所以:

$("#select").change(function() { 
    var thes = $(this).attr('value'); 
    var next = $('option:selected').next().attr('value'); 
    alert(next); 
}); 

檢查this fiddle

請注意,這將給next()最後的索引爲undefined。你可能想要適當地處理。

閱讀來自:selected selector here

0

你錯過了這個

var next = $('option:selected').next().attr('value'); 

JSFIddle