javascript如何点击行,选中该行的checkbox。点击其它行选中其它行,只选中一行。不用jquery
<table>
<tr id="1" onclick="Get(this.id)">
<td><input type="checkbox" name="cb" id="Checkbox3" class="cb" /></td>
<td style="display: none">11</td>
</tr>
<tr id="2" onclick="Get(this.id)">
<td><input type="checkbox" name="cb" id="Checkbox3" class="cb" /></td>
<td style="display: none">11</td>
</tr>
<tr id="3" onclick="Get(this.id)">
<td><input type="checkbox" name="cb" id="Checkbox3" class="cb" /></td>
<td style="display: none">11</td>
</tr>
</table>
<table>
<tr id="1" onclick="Get(this.id)">
<td><input type="checkbox" name="cb" id="Checkbox3" class="cb" /></td>
<td style="display: none">11</td>
</tr>
<tr id="2" onclick="Get(this.id)">
<td><input type="checkbox" name="cb" id="Checkbox3" class="cb" /></td>
<td style="display: none">11</td>
</tr>
<tr id="3" onclick="Get(this.id)">
<td><input type="checkbox" name="cb" id="Checkbox3" class="cb" /></td>
<td style="display: none">11</td>
</tr>
</table>
var checkbox = document.getElementsById("LayoutTable").getElementsByTagName("input");
for(var i=0,length=checkbox.length;i<length;i++){
if(checkbox.type == 'checkbox'){
//checkbox表单
id = checkbox[i].
}
var checkbox = document.getElementByName("cb");
for(var i = 0;i < checkbox.length; i++) {
checkbox[i].checked = false;
}
ele.childNodes[0].childNodes[0].checked=true;
}
var checkbox = document.getElementsByName("cb");
for(var i = 0;i < checkbox.length; i++) {
checkbox[i].checked = false;
}
ele.childNodes[0].childNodes[0].checked=true;
}
上面的有错。
假设为变量cb
获取id这样
cb.parentNode.parentNode.id
var checkbox = document.getElementsByName("cb");
for (var i = 0; i < checkbox.length; i++) {
if( checkbox[i].checked ==true)
{
//在这里怎样获取checkbox所在tr的id。parentNode?
}
}
var checkbox = document.getElementsByName("cb");
for (var i = 0; i < checkbox.length; i++) {
if( checkbox[i].checked ==true)
{
id= checkbox[i].parentNode.parentNode.id;
}
}
this.parentNode.firstChild.chekced=true;
}