jQuery从选定的下拉列表中获取html id [英] jQuery get html id from selected dropdown

查看:88
本文介绍了jQuery从选定的下拉列表中获取html id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

As i have multiple drop-downs in my form, I would like to retrieve the HTML id from one of the selected drop-downs. I have the following code for my drop-downs on change:

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

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

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

i can see the selected drop-down id in the console;

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

What is the syntax for retrieving this id into a var?

推荐答案

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

您也可以使用。 id (正如评论中已经提到的那样)。我刚发现性能测试 $(this).attr(id) this.id 结果为 this.id 更快,这是预期的,因为它是纯javascript而不是像jQuery这样的javascript库。

You can also use this.id (as already mentioned in the comments). I just found a performance test for $(this).attr("id") vs. this.id with the result of this.id being faster, which is expected as it's pure javascript instead of a javascript library like jQuery.

这篇关于jQuery从选定的下拉列表中获取html id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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