简体   繁体   English

发出将addEventListener事件添加到documentFragment中的Input的问题

[英]Issue adding event with addEventListener to Input in documentFragment

I am creating a div using createDocumentFragment(). 我正在使用createDocumentFragment()创建一个div。 In the Div is a table with a list of input. Div中的表是输入列表。 When you click on any of the checkboxes I want to trigger the alert('Yes'). 当您单击任何复选框时,我要触发警报(“是”)。 when I add the event it does not add it to the input but with Firefox it seems to call the alert when it is added to the table. 当我添加事件时,它不会将其添加到输入中,但是对于Firefox,当将其添加到表中时,它似乎会调用警报。

Can someone explain what I am doing wrong? 有人可以解释我在做什么错吗?


function deptFilter(rPattern)
{       

var lclData = JSON.parse(rPattern);                 
var  loc = document.getElementById('show2');                
var arrayKeys = Object.keys(lclData);               

    var outputData;
    var LCL_List
    var LCLTables;
    var LCLtd;
    var LCLtr;
    var LCLInput;
    var LCLDiv;



    var LCL_List = document.createDocumentFragment(document.createElement('DIV'));
    LCL_List.id = 'LCLTable';



for(var x = 0; x < arrayKeys.length; x++)
{

LCLDiv = LCL_List.appendChild(document.createElement('DIV'));
LCLTables = LCLDiv.appendChild(document.createElement('TABLE'));                                

                for(var y = 0; y < lclData[arrayKeys[x]].length; y++)
                {

                    LCLtr = LCLTables.appendChild(document.createElement('TR'));
                    LCLtd = LCLtr.appendChild(document.createElement('TD'));
                    LCLInput = LCLtd.appendChild(document.createElement('INPUT'));
                    LCLInput.id = lclData[arrayKeys[x]][y]['Name'];
                    LCLInput.type='checkbox';
                    LCLInput.addEventListener("click", alert("Yes"));       


                }   



        }


        loc.appendChild(LCL_List);



}

This is the way to add event listner: 这是添加事件列表器的方法:

LCLInput.addEventListener("click", function() {
    alert("Yes");
});

When you install an event handler like this: 当您安装这样的事件处理程序时:

LCLInput.addEventListener("click", alert("Yes"));  

You're executing alert() immediately and then passing the return value from that to addEventListener() . 您正在立即执行alert() ,然后将其返回值传递给addEventListener() This is obviously NOT what you want. 这显然不是您想要的。 Instead, you need to pass a function reference to addEventListener and that function will then call your alert() sometime later: 相反,您需要将函数引用传递给addEventListener,然后该函数将在稍后的某个时间调用您的alert()

LCLInput.addEventListener("click", function(e) {
    alert("Yes");
});  

Or you can define a named function and pass just its name: 或者,您可以定义一个命名函数并仅传递其名称:

function handleClick(e) {
    alert("Yes");
}

LCLInput.addEventListener("click", handleClick);  

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

相关问题 添加到 dom [object DocumentFragment] 问题 - Adding to dom [object DocumentFragment] problem 为什么我的 foreach addEventListener 没有添加事件? - Why does my foreach addEventListener is not adding the event? 使用addEventListener将JavaScript添加到表单时出现问题 - Issue adding javascript to a form using addEventListener 无法将事件传递给addEventListener:closure问题 - Can't pass event to addEventListener: closure issue 使用 addEventListener 管理事件,并使用 querySelectorAll 选择动态输入 - manage event, with addEventListener, and select dynamic input with querySelectorAll 在使用addEventListener添加事件之前,请使用removeEventListener删除事件不起作用 - remove an event using removeEventListener before adding an event using addEventListener is not working addEventListener 的问题 - Issue with an addEventListener 通过 addEventListener 添加点击事件以确认从超链接导航 - Adding click event via addEventListener to confirm navigation from a hyperlink 如何使用`addEventListener`中的模糊事件获取所有表单输入值? - How to get all form input values using blur event in `addEventListener`? 按键事件的addEventListener中的问题……仅用于限制输入字符 - problem in addEventListener for keypress event… for restricting the input charcters only
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM