简体   繁体   English

Java中DOM的onchange不起作用

[英]onchange of DOM in Javascript not working

var nameInput = document.createElement("input");
nameInput.type = "text";
nameInput.className = "loop_input";
nameInput.id = "nameAddress"+validLoadedEmails;
nameInput.value = data[0];
nameInput.onchange = editAddressHidden(validLoadedEmails);

it seems that when creating the element. 似乎在创建元素时。 It will already go to the editAddressHidden function and not when there's a change. 它将已经进入editAddressHidden函数,而不是进行更改。

Given your current code: 根据您当前的代码:

nameInput.onchange = editAddressHidden(validLoadedEmails);

You can think of this as saying "handle the onchange event of nameInput with whatever's returned from my editAddressHidden function". 您可以认为这是“用我的editAddressHidden函数返回的值处理nameInputonchange事件”。 But that's not right (in this instance), as you want to handle the onchange event with the editAddressHidden function itself. 但这不对(在这种情况下),因为您想使用editAddressHidden函数本身来处理onchange事件。

So wrap it up in an anonymous function: 因此,将其包装在一个匿名函数中:

nameInput.onchange = function(){ editAddressHidden(validLoadedEmails) };

Now every time the onchange event fires, it will invoke your function, and this will in turn call the editAddressHidden function. 现在,每次onchange事件触发时,它将调用您的函数,这将依次调用editAddressHidden函数。

Use this 用这个

nameInput.onchange = function(){ editAddressHidden(validLoadedEmails) };

Because you call editAddressHidden function and assign returned value as input's change handler, that is not what you wanted. 因为您调用editAddressHidden函数并将返回值分配为输入的更改处理程序,所以这不是您想要的。

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

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