简体   繁体   English

未通过动态创建的复选框触发操作

[英]Action not triggered with checkbox created dynamically

When I created a checkbox with HTML the action is triggered normal but the problem when I create the checkbox with javascript(when I add a row in my table with button and I click to the new checkbox) action does not work. 当我创建带有HTML的复选框时,该动作被正常触发,但是当我使用javascript创建该复选框时(当我在带有按钮的表中添加一行并单击到新的复选框时),该问题不起作用。

This is my code : 这是我的代码:

<HTML>
<HEAD>

<SCRIPT type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<SCRIPT>


    $(window).load(function() {

        $('input[name=chk]').change(function() { 
            if ($('input[name=chk]').is(':checked')) {
                alert("checked");   
                }
            else {
                alert("unchecked"); 
            }
        }); 
    });

    function addRow(tableID) {

        var table = document.getElementById(tableID);

        var rowCount = table.rows.length;
        var row = table.insertRow(rowCount);

        var cell1 = row.insertCell(0);
        var element1 = document.createElement("input");
        element1.type = "checkbox";
        element1.setAttribute("name","chk");
        cell1.appendChild(element1);

        var cell2 = row.insertCell(1);
        cell2.innerHTML = rowCount + 1;

        var cell3 = row.insertCell(2);
        var element2 = document.createElement("input");
        element2.type = "text";
        cell3.appendChild(element2);
    }


</SCRIPT>
</HEAD>
<BODY>

<INPUT type="button" value="Add Row" onclick="addRow('dataTable')" />

<TABLE id="dataTable" width="350px" border="1">
    <TR>
        <TD><INPUT type="checkbox" name="chk"/></TD>
        <TD> 1 </TD>
        <TD> <INPUT type="text" /> </TD>
    </TR>
</TABLE>

</BODY>
</HTML>

Thank you 谢谢

That's because your event handler: 那是因为您的事件处理程序:

$('input[name=chk]').change(function() { ... });

...is applied only to those inputs that match the selector at that moment - before the dynamically created inputs exist. ...仅应用于当时与选择器匹配的那些输入-在动态创建的输入存在之前。 You can instead use a delegated event handler that is attached to a parent element (or to the document) that checks the selector at the time the event occurs: 相反,您可以使用附加到父元素(或文档)的委托事件处理程序,该事件处理程序在事件发生时检查选择器:

$('#dataTable').on('change', 'input[name=chk]', function() { ... });

The .on() method was added in version 1.7, so if you're using an older version of jQuery use .delegate() instead. .on()方法是在1.7版中添加的,因此,如果您使用的是旧版jQuery,请改用.delegate() (Unless you're using a really old version, pre 1.4.2, in which case you'll have to use .live() .) (除非您使用的 1.4.2之前的旧版本,否则必须使用.live() 。)

For dynamically generated elements, events should be delegated from one of static parents of the element or document object, you can use the on method. 对于动态生成的元素,应从元素或文档对象的静态父级之一委托事件,可以使用on方法。

$('#dataTable').on('click', 'input[name=chk]', function(){
   // ...
})

The problem here is that you'r trying to attach an event listener (the .change() ) to an element that doesn't exist yet in the DOM. 这里的问题是您试图将事件侦听器( .change() )附加到DOM中尚不存在的元素。 You should bind the listener only after creating the chackbox. 您仅应在创建chackbox之后绑定侦听器。

You should be using the on() method instead, provided you are using jquery 1.7 or above 如果您使用的是jQuery 1.7或更高版本,则应改用on()方法

See working fiddle 查看工作提琴

    $('#dataTable').on('click','input:checkbox',function() { 
        if ($('input[name=chk]').is(':checked')) {
            alert("checked");   
            }
        else {
            alert("unchecked"); 
        }
    }); 

You need to set a .live handler for your checkboxes. 您需要为复选框设置一个.live处理程序。 Also, in the handler, you can use $(this) to get current checkbox. 另外,在处理程序中,您可以使用$(this)获取当前复选框。

Here you are: http://jsfiddle.net/edAv8/ 您在这里: http : //jsfiddle.net/edAv8/

Or you may use .on handler: http://jsfiddle.net/edAv8/1/ 或者您可以使用.on处理程序: http : //jsfiddle.net/edAv8/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM