let x_rows = 2;
addNewRow = () => {
let bundle_of_rows = document.createElement("div");
while (x_rows - 1 >= 0) {
let a_row = document.createElement("input");
bundle_of_rows.appendChild(a_row);
x_rows--;
}
document.getElementById("sub-main").appendChild(bundle_of_rows);
};
这段代码会创建这样的东西......
<div>
<input></input>
<input></input>
</div>
然后它将它附加到“sub-main”
<div id="sub-main">
<div>
<input></input>
<input></input>
</div>
</div>
我想存储“输入”的值,比如应该更新 this.state = {input: ""} 的 onChange。有没有办法为这些输入赋予属性?/ 必须使用 createElement() /
将其作为属性添加到您创建的a_row
对象中:
let a_row = document.createElement("input");
a_row.onChange = () => {
console.log('do onchange things');
};
或者你也可以添加一个事件监听器:
a_row.addEventListener('change', () => {
console.log('do onchange things');
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句