使用 jQuery 获取选定的选项 ID [英] Get the selected option id with jQuery
本文介绍了使用 jQuery 获取选定的选项 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用 jQuery 根据所选选项发出 ajax 请求.
I'm trying to use jQuery to make an ajax request based on a selected option.
是否有一种简单的方法可以使用 jQuery 检索所选的选项 id(例如id2")?
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
});
推荐答案
您可以使用 :selected
选择器,像这样:
You can get it using the :selected
selector, like this:
$("#my_select").change(function() {
var id = $(this).children(":selected").attr("id");
});
这篇关于使用 jQuery 获取选定的选项 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文