2013-10-18 66 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