[英]How do I attach an onchange() event into a dynamically created input text field?
I am trying to attach an onchange()
listener into my input
text field, inside my table like this:我正在尝试将一个
onchange()
侦听器附加到我的input
文本字段中,在我的表中,如下所示:
var table = document.getElementById("employees");
var input = document.createElement("input");
input.type = "text";
var row = table.insertRow(0);
var cell1 = row.insertCell(0);
var cell2=row.insertCell(1);
cell1.innerHTML="<b>Name:</b>";
cell2.appendChild(input);
input.addEventListener("change",find_emp(input.value)); //not working
input.onchange=find_emp(input.value); //still not working
But the onchange()
event is not working.但是
onchange()
事件不起作用。 What do I do in order to make it work?我该怎么做才能让它发挥作用?
You could use an arrow function for both events change
and keyup
like:您可以对事件
change
和keyup
使用箭头函数,例如:
var table = document.getElementById("employees"); var input = document.createElement("input"); input.type = "text"; var row = table.insertRow(0); var cell1 = row.insertCell(0); var cell2=row.insertCell(1); cell1.innerHTML="<b>Name:</b>"; cell2.appendChild(input); input.addEventListener("change",() => console.log("from change", input.value)); input.addEventListener("keyup",() => console.log("from keyup", input.value));
<table id="employees" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.