[英]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
函数返回的值处理nameInput
的onchange
事件”。 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.