javascirpt能否操作表格中的对象

coolria 2011-07-06 11:04:38
我的表格中每行会放一个checkbox,一个input,我是通过

cell.innerHTML="<input type=\"checkbox\" id=\"cb\"value=\"123\">";
//所有行的ID都是cb

来构建对象的

现在我想遍历表格,把所有input值等于str的所在行的checkbox都选上,应该怎么做呢?能不用.innerHTML来修改吗,能不能直接操作对象属性?
...全文
88 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
licip 2011-07-06
  • 打赏
  • 举报
回复
+1[Quote=引用 1 楼 a276786511 的回复:]
<table id="tb">
<tr><td><input type="checkbox" /><input type="text" value="123"></td></tr>
<tr><td><input type="checkbox" /><input type="text" value="123"></td></tr>
<tr><td><input type="checkbox" ……
[/Quote]
挨踢直男 2011-07-06
  • 打赏
  • 举报
回复

<table id="tb">
<tr>
<td><input type="checkbox" value="123" /></td>
<td><input type="checkbox" value="1231" /></td>
</tr>
<tr>
<td><input type="checkbox" value="123" /></td>
<td><input type="checkbox" value="1231" /></td>
</tr>
<tr>
<td><input type="checkbox" value="123" /></td>
<td><input type="checkbox" value="1231" /></td>
</tr>
<tr>
<td><input type="checkbox" value="123" /></td>
<td><input type="checkbox" value="1231" /></td>
</tr>
<tr>
<td><input type="checkbox" value="123" /></td>
<td><input type="checkbox" value="1231" /></td>
</tr>
</table>

<script type="text/javascript">
var str = "123"
var checkboxes = document.getElementById("tb").getElementsByTagName("input")
for(var i = 0 ; i < checkboxes.length; i ++)
{
if(checkboxes[i].type=="checkbox" && checkboxes[i].value == str)
{
checkboxes[i].checked = true;
}
}
</script>
a276786511 2011-07-06
  • 打赏
  • 举报
回复
<table id="tb">
<tr><td><input type="checkbox" /><input type="text" value="123"></td></tr>
<tr><td><input type="checkbox" /><input type="text" value="123"></td></tr>
<tr><td><input type="checkbox" /><input type="text" value="456"></td></tr>
<tr><td><input type="checkbox" /><input type="text" value="123"></td></tr>
</table>
<script>
var tb = document.getElementById("tb")
for(i=0;i<tb.rows.length;i++){
if(tb.rows[i].cells[0].childNodes[1].value=="123"){
tb.rows[i].cells[0].childNodes[0].checked=true;
}
}
</script>

87,990

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧