我的表格中每行会放一个checkbox,一个input,我是通过cell.innerHTML="<input type=\"checkbox\" id=\"cb\"value=\"123\">";
//所有行的ID都是cb来构建对象的现在我想遍历表格,把所有input值等于str的所在行的checkbox都选上,应该怎么做呢?能不用.innerHTML来修改吗,能不能直接操作对象属性?

解决方案 »

  1.   

    <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>
      

  2.   


    <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>