jQuery从选定的下拉列表中获取html id

时间:2014-10-24 20:09:15

标签: javascript jquery

由于我的表单中有多个下拉列表,因此我想从其中一个选定的下拉列表中检索HTML ID。我有关于变更的下拉列表的以下代码:

$("select[name$='product_type']").change(function(){}

使用console.log($(this).select());

我可以在控制台中看到所选的下拉ID;

enter image description here

将此ID检索到var?

的语法是什么?

3 个答案:

答案 0 :(得分:4)

只需使用$(this).attr("id")即可获取ID。

您也可以使用this.id(已在评论中提及)。我刚刚为$(this).attr("id")this.id找到performance testthis.id的结果更快,因为它是纯javascript而不是javascript库像jQuery。

答案 1 :(得分:1)

你只需要id财产:

$("select[name$='product_type']").change(function() {
    console.log(this.id);
});

答案 2 :(得分:-2)

更改回调中的

$("select[name$='product_type'] option:selected").attr("id");