由于我的表单中有多个下拉列表,因此我想从其中一个选定的下拉列表中检索HTML ID。我有关于变更的下拉列表的以下代码:
$("select[name$='product_type']").change(function(){}
使用console.log($(this).select());
我可以在控制台中看到所选的下拉ID;
将此ID检索到var?
的语法是什么?答案 0 :(得分:4)
只需使用$(this).attr("id")
即可获取ID。
您也可以使用this.id
(已在评论中提及)。我刚刚为$(this).attr("id")
与this.id
找到performance test,this.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");
。