我正在尝试使用jQuery根据选择的选项发出ajax请求。
有没有一种简单的方法可以使用jQuery检索所选的选项id (例如"id2")?
<select id="my_select">
<option value="o1" id="id1">Option1</option>
<option value="o2" id="id2">Option2</option>
</select>
$("#my_select").change(function() {
//do something with the id of the selected option
});
发布于 2010-05-22 22:30:16
您可以使用:selected
selector获取它,如下所示:
$("#my_select").change(function() {
var id = $(this).children(":selected").attr("id");
});
发布于 2010-05-22 22:31:50
var id = $(this).find('option:selected').attr('id');
然后你可以对selectedIndex
做任何你想做的事情
我重新修改了我的答案...由于selectedIndex不是一个很好的变量来举例...
发布于 2013-11-13 13:28:32
$('#my_select option:selected').attr('id');
https://stackoverflow.com/questions/2888446
复制相似问题