【问题标题】:jQuery .on() does not trigger any events [duplicate]jQuery .on() 不会触发任何事件 [重复]
【发布时间】:2013-05-26 02:50:06
【问题描述】:

我正在使用 jQuery 编写一个应用程序,当单击一个按钮时,会生成一个选择框并附加到一行。 Whenever the select changes, it should trigger the change event, but using .on("change", function() {}); isn't working:

代码:

<!DOCTYPE html>
<html>
    <head>
        <title>HTML5-Template</title>
        <meta http-equiv="content-type" content="text/html; charset=utf-8">
        <style type="text/css">
            html, body
            {
            }

            input,select {
                border: 1px solid #404048;
                padding: 4px;
            }
        </style>
        <script type="text/javascript" src="../JavaScript/JQuery/jquery-1.9.1.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                $("#cmdCreateRow").click(function() {
                    var row = $("<div />")
                    .css("font", "normal 12px Verdana")
                    .css("padding", "8px")
                    .css("display", "inline-block")
                    .css("background", "#F0F0F8")
                    .css("border", "1px solid #A0A0A8")
                    .css("margin", "2px");

                    var types = [
                        "Local", 
                        "Remote",
                        "(Custom)"
                    ];

                    var type = $("<select />").attr("class", "member-type");
                    for(var i in types) {
                        type.append($("<option />").val(types[i]).text(types[i]));
                    };

                    row.append(type);

                    $("#Container").append(row).append($("<br />"));
                });

                $(".member-type").on("change", function() {
                    alert("changed");
                });
            });
        </script>
    </head>
    <body>
        <input type="button" id="cmdCreateRow" value="Create Row" />
        <div id="Container">
        </div>
    </body>
</html>

知道我做错了什么吗?

【问题讨论】:

    标签: javascript jquery html


    【解决方案1】:

    当您进行绑定时,$(".member-type") 为空,因此您什么也没做。

    改变

    $(".member-type").on("change", function() {
           alert("changed");
    });
    

    $(document).on("change", ".member-type", function() {
             alert("changed");
    });
    

    或者这是因为#Container 元素是静态的:

    $("#Container").on("change", ".member-type", function() {
             alert("changed");
    });
    

    【讨论】:

    • 工作中...谢谢! :-)
    • 我喜欢您作为社区 wiki 回答这些问题。 Good Guy Dystroy FTW。
    【解决方案2】:

    试试这个:

    $(document).on("change", ".member-type", function() {
        //Code
    });
    

    这是 jQuery 中“live”的替代品,请参阅:http://api.jquery.com/live/

    --

    常规的“on”方法仅适用于 DOM 中已经存在的元素。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-04
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-22
      • 1970-01-01
      相关资源
      最近更新 更多