我已经动态生成了这个复选框元素,我正在尝试动态检查它。
这是我的做法:
var id = "123"; //dynamically generated
var input = document.createElement("input");
input.setAttribute("type", "checkbox");
input.setAttribute("name", "x_"+id);
input.setAttribute("id", "x_"+id);
input.setAttribute("value", "");
input.setAttribute("checked",true); //This isn't working
var elem = document.getElementById('x_' + id);
elem.checked = true; //This isn't working
最佳答案
您实际上从未将元素添加到页面中。它在这里工作,只需要做:
document.body.appendChild(input);
var id = "123"
var input = document.createElement("input");
input.setAttribute("type", "checkbox");
input.setAttribute("name", "x_"+id);
input.setAttribute("id", "x_"+id);
input.setAttribute("value", "");
input.setAttribute("checked",true); //This isn't working
document.body.appendChild(input);
var elem = document.getElementById('x_' + id);
elem.checked = true; //This isn't working`enter code here`
关于javascript - 我无法检查输入复选框元素,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36657467/