简体   繁体   English

JQuery .on(“click”,function())不起作用

[英]JQuery .on(“click”, function() ) doesn't work

I am trying to fire JQuery when I checkbox is checked. 当我选中复选框时,我试图解雇JQuery。 At first I realized my JQuery only works for static elements. 起初我意识到我的JQuery只适用于静态元素。 I read through a couple posts and found out that I need .on("click, function()) in order to fire that same piece of javascript for dynamically added elements. 我阅读了几篇文章,发现我需要.on(“click,function())才能为动态添加的元素触发相同的javascript。

However, this method still doesn't work for me. 但是,这种方法对我来说仍然不起作用。 Can anyone help? 有人可以帮忙吗? Thank you. 谢谢。

$(document).ready(function(){
    $("input[name='todo']").on('click', function(){
    var isChecked = this.checked
           if (isChecked == true){
          $(this).next().remove();
          $(this).remove(); 
        }
        if (isChecked == false)
        {
        alert("checkbox is NOT checked");
        }

        });

    });

My example app: http://jsfiddle.net/JSFoo/7sK7T/8/ 我的示例应用程序: http//jsfiddle.net/JSFoo/7sK7T/8/

You need delegation: 你需要代表团:

$('#ToDoList').on('click', "input[name='todo']", function() { ... });

http://jsfiddle.net/7sK7T/9/ http://jsfiddle.net/7sK7T/9/

Documentation: http://api.jquery.com/on/#direct-and-delegated-events 文档: http//api.jquery.com/on/#direct-and-delegated-events

PS: the important note - you need to specify the element you're binding your handler to as close to the target elements as possible. PS:重要的注意事项 - 您需要将处理程序绑定的元素指定为尽可能接近目标元素。 In your case it's #ToDoList , not body or document as other answers advice. 在你的情况下,它是#ToDoList ,而不是其他答案建议的bodydocument

For dynamic elements you would want to do something like this 对于动态元素,您可能希望执行此类操作

$(document).ready(function () {
    $(document).on('click', "input[name='todo']", function () {
        var isChecked = this.checked
        if (isChecked == true) {
            $(this).next().remove();
            $(this).remove();
        }
        if (isChecked == false) {
            alert("checkbox is NOT checked");
        }

    });

});

if you use it like you were before on('click') is basically the same as click(); 如果你像以前一样使用它on('click')基本上与click();相同click(); because you are still selecting the elements required and applying the event to those elements, the way the above example works is it is only applies the event to the document then checking the selector when the event is fired. 因为您仍在选择所需的元素并将事件应用于这些元素,所以上述示例的工作方式是仅将事件应用于document然后在触发事件时检查选择器。

You can also change document to a close container of the elements to be clicked if you wish. 如果您愿意,还可以将document更改为要单击的元素的关闭容器。

This is called Event Delegation 这称为事件委派

jQuery Learn Event Delegation jQuery学习事件委派

The below is what you needed. 以下是您所需要的。 It is slightly different syntax 它的语法略有不同

$(document).on('click', "input[name='todo']", function(){

You bind the elements on document ready, it's before they're created. 您可以在文档准备就绪之前绑定它们,这是在它们创建之前。 You have to bind AFTER their creation. 你必须在他们的创作后绑定。

Alternatively you can bind their container on document.ready: 或者,您可以在document.ready上绑定它们的容器:

$("#containerDiv").on('click', "input[name='todo']", function(){
   // .... your code
});
  • "containerDiv" should be the parent element of those checkboxes, it should be in the page on document ready! “containerDiv”应该是那些复选框的父元素,它应该在文档准备好的页面中!

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

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