Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery从所选下拉列表中获取html id_Javascript_Jquery - Fatal编程技术网

Javascript jQuery从所选下拉列表中获取html id

Javascript jQuery从所选下拉列表中获取html id,javascript,jquery,Javascript,Jquery,由于我的表单中有多个下拉列表,我想从其中一个选定的下拉列表中检索HTML id。我在更改下拉列表中有以下代码: $("select[name$='product_type']").change(function(){} 使用console.log($(this.select())时 我可以在控制台中看到所选的下拉id 将此id检索到var中的语法是什么?只需使用$(this).attr(“id”)即可获取id 您还可以使用this.id(如注释中所述)。我刚刚找到了一个for$(this.a

由于我的表单中有多个下拉列表,我想从其中一个选定的下拉列表中检索HTML id。我在更改下拉列表中有以下代码:

$("select[name$='product_type']").change(function(){}
使用
console.log($(this.select())时

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

将此id检索到var中的语法是什么?

只需使用
$(this).attr(“id”)
即可获取id


您还可以使用
this.id
(如注释中所述)。我刚刚找到了一个for
$(this.attr(“id”)
this.id
的比较,结果是
this.id
更快,这是预期的,因为它是纯javascript,而不是像jQuery这样的javascript库。

您只需获取
id
属性:

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

$(“选择[name$='product_type']选项:选中”).attr(“id”)
在更改回调中。

此.id也将起作用。works,ty将尽快接受(10分钟窗口)