简体   繁体   English

对于动态创建的复选框,单击事件处理程序不保留

[英]Click event handler not persisting for dynamically created checkboxes

I have a Javascript script in which I define a class called a "Layer". 我有一个Javascript脚本,其中定义了一个称为“图层”的类。 I put code in the Layer constructor, so that each time a new Layer object is created a checkbox associated with that layer is added to the HTML document. 我将代码放入Layer构造函数中,以便每次创建新的Layer对象时,与该图层关联的复选框都会添加到HTML文档中。 I also created a JQuery event handler within the constructor so that when the new checkbox is clicked, the layer is either shown or hidden within the document depending on the state of the checkbox. 我还在构造函数中创建了一个JQuery事件处理程序,以便在单击新复选框时,根据复选框的状态在文档中显示或隐藏该层。 For some reason however, the event handler only works for the most recently created layer. 但是由于某种原因,事件处理程序仅适用于最近创建的图层。 Somehow the old event handlers are getting overwritten. 不知何故,旧的事件处理程序已被覆盖。 I searched on here for similar questions, and saw that using the on function from JQuery rather than click is the way to go, but I'm still having the same issue. 我在这里搜索了类似的问题,发现使用JQuery的on函数而不是click是解决问题的方法,但是我仍然遇到同样的问题。 Here's my code... 这是我的代码...

Layer2D.prototype.addToUI = function() {
    if (this.firstLayer()) {
        var layerDiv = document.createElement('div');
        layerDiv.className = 'content';
        layerDiv.id = 'layerNames';
    }
    else {
        var layerDiv = document.getElementById('layerNames');
    }

    layerDiv.innerHTML += '</br>' +
                          '<div class="ui toggle mini checkbox" id="layer' + this.name +'">' +
                          '<input type="checkbox" name="' + this.name + '" checked>' +
                          '</input>' +
                          '<label>' + this.name + '</label>' +
                          '</div>';

    if (this.firstLayer()) {
        var parentDiv = document.getElementById('layerMenu');
        parentDiv.appendChild(layerDiv);
    };
    $('#layer' + this.name).after('<br/>');

    var This = this;
    $('#layer' + this.name).on('click', ':input', function(e) {
        checked = $(this).is(':checked');
        console.log(This);
        if (checked === true) {
            This.show();
        }
        else { 
            This.hide();
        }
    });
};

您应该使用$(document).on('click','#layer'+ this.name,function(){...}),它通常与动态添加的HTML一起使用。

innerHTML has this issue. innerHTML存在此问题。 If you create the dynamic elements using Document.create methods, I think the listener will be attached. 如果使用Document.create方法创建动态元素,我认为将附加侦听器。 In any case you can put your evenListener(s) in a function like addEventListenerToBeer() and call it after the innerHTML call. 无论如何,您都可以将evenListener放在类似addEventListenerToBeer()的函数中,并在innerHTML调用之后进行调用。

var d = document.createElement("div")
var t = document.createTextNode("i like beer");
d.id = "myId";
d.appendChild(t); 

You need to replace: 您需要更换:

layerDiv.innerHTML += '</br>' +
                      '<div class="ui toggle mini checkbox" id="layer' + this.name +'">' +
                      '<input type="checkbox" name="' + this.name + '" checked>' +
                      '</input>' +
                      '<label>' + this.name + '</label>' +
                      '</div>';

for: 对于:

let element="";
//****
element = '</br>' +
                      '<div class="ui toggle mini checkbox" id="layer' + this.name +'">' +
                      '<input type="checkbox" name="' + this.name + '" checked>' +
                      '</input>' +
                      '<label>' + this.name + '</label>' +
                      '</div>';
$(element).appendTo(layerDiv);

appendTo inserts the elements and make them part of the DOM to be query... appendTo插入元素并使它们成为要查询的DOM的一部分...

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

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