【问题标题】:jQuery get html id from selected dropdown [duplicate]jQuery从选定的下拉列表中获取html id [重复]
【发布时间】:2014-12-20 17:42:31
【问题描述】:

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

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

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

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

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

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

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

    您也可以使用this.id(如 cmets 中已经提到的)。我刚刚为$(this).attr("id")this.id 找到了performance test,结果this.id 更快,这是预期的,因为它是纯javascript,而不是像jQuery 这样的javascript 库。

    【讨论】:

    • this.id 也可以。
    • 作品,你,我会尽快接受(10 分钟窗口)
    【解决方案2】:

    你只需要id属性:

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

    【讨论】:

      【解决方案3】:

      $("select[name$='product_type'] option:selected").attr("id"); 在更改回调中。

      【讨论】:

        猜你喜欢
        • 2015-07-21
        • 1970-01-01
        • 2022-01-21
        • 1970-01-01
        • 1970-01-01
        • 2016-08-28
        • 1970-01-01
        相关资源
        最近更新 更多