[英]Checking if dynamically created checkbox is selected using javascript
I have a table containing checkboxes. 我有一个包含复选框的表。 I add checkboxes to a table whenever a button is clicked as follows:
每当单击按钮时,都会向表中添加复选框,如下所示:
var cell3 = row.insertCell(2);
cell3.innerHTML = '<input type="checkBox" value=\"selected?\" style="cursor:pointer" value="htcb"/>';
How can I find out if the checkbox in, say cell 3, is selected? 我如何确定是否选中了复选框(例如单元格3)?
I've tried this 我已经试过了
var myCheckBox = row.cells[2].innerHTML;
if(myCheckBox .checked == true)
//
but it doesn't work 但这不起作用
The innerHTML
property is just a string, you need to access the DOM object itself. innerHTML
属性只是一个字符串,您需要访问DOM对象本身。
Seeing as the <input>
is the only thing in the cell, this should work: 看到
<input>
是单元格中唯一的东西,这应该可以工作:
var myCheckBox = row.cells[2].firstChild;
if (myCheckBox.checked) {
...
}
js function: js函数:
function check() {
if($(this).is(”:checked”))
{
alert(’checked’);
}
}
you must add onchange listener: 您必须添加onchange侦听器:
cell3.innerHTML = '<input type="checkBox" value=\"selected?\"
style="cursor:pointer"value="htcb" onchange="check()"/>';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.