使用jQuery获取选定的选项ID [英] Get the selected option id with jQuery

查看:116
本文介绍了使用jQuery获取选定的选项ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



是否有一种简单的方法来检索所选的选项ID (例如id2)使用jQuery?

 < select id =my_select> 
< option value =o1id =id1> Option1< / option>
< option value =o2id =id2> Option2< / option>
< / select>


$(#my_select)。change(function(){
//用所选项的id
}做某事);


解决方案

您可以使用 :selected selector ,如下所示:

  $(#my_select)。change(function(){
var id = $(this).children ).attr(id);
});


I'm trying to use jQuery to make an ajax request based on a selected option.

Is there a simple way to retrieve the selected option id (e.g. "id2") using jQuery?

<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
});

解决方案

You can get it using the :selected selector, like this:

$("#my_select").change(function() {
  var id = $(this).children(":selected").attr("id");
});

这篇关于使用jQuery获取选定的选项ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆