<script>
function test(obj) {    document.getElementById('cb' + obj.name.substring(2)).checked = true;
}
function toggleSelect(flag) {
    if(flag == 1) {        for(var i=1;i<3;i++) {            document.getElementById('cb' + i).checked = true;
        }
    } else {        for(var i=1;i<3;i++) {            document.getElementById('cb' + i).checked = false;
        }
    }
}
</script>
print '<table class="stripe_tb" ><caption>用 户 列 表</caption>
<tr><th></th><th>学 号</th><th>姓 名</th><th>性 别</th><th>专 业</th><th>年 级</th><th>班 级</th><th>编 辑</th><th>删 除</th></th></tr>';
while(@$print = $db->fetch_array()){
echo '<tr style="cursor:pointer;" name = "tr" id="tr1" onclick="test(this)">
<td><input type="checkbox" name="cb1" id="'.$print['user_id'].'"></td>
<td style="cursor:pointer;">'.$print['user_id'].'</td>
<td style="cursor:pointer;" >'.$print['user_name'].'</td>
<td style="cursor:pointer;" >'.$print['user_sex'].'</td>
<td style="cursor:pointer;" >'.$print['user_science'].'</td>
<td style="cursor:pointer;" >'.$print['user_grade'].'</td>
<td style="cursor:pointer;" >'.$print['user_class'].'</td>
<td class="center"><a href="edit_user.php?user_id='.$print['user_id'].'" title="编辑"><img src="images/edit.png"></a></td>
<td class="center"><a href="del_user.php?user_id='.$print['user_id'].'" title="删除"><img src="images/drop.png"  onclick="return confirm(\'确定删除该用户吗?\')"></a></td>
</tr>';
}
以上代码是昨天CSDN一个朋友提供的,我把ID做了相应的修改,但是还是没有实现我想要的方法,现还是没有实现我想要的方法,我需要的方法是,点击第几条数据,会选中这条数据相应的checkbox,但是现在不管我点击第几条数据,都只会选中第一条数据,朋友们帮忙改改.谢谢了~

解决方案 »

  1.   

    <script type="text/javascript">
    function checked(id)
    {
    var $ = document.getElementById;
    if($(id).checked)
    {
    $(id).checked = false;
    }
    else $(id).checked = true;
    }
    </script>
    <table border=1>
    <tr onclick="checked('row1')" style="cursor:pointer;" ><td><input type="checkbox" name="row[]" id="row1"></td><td>春天</td><td>风</td></tr>
    <tr onclick="checked('row2')" style="cursor:pointer;" ><td><input type="checkbox" name="row[]" id="row2"></td><td>夏天</td><td>雨</td></tr>
    <tr onclick="checked('row3')" style="cursor:pointer;" ><td><input type="checkbox" name="row[]" id="row3"></td><td>秋天</td><td>雪</td></tr>
    <tr onclick="checked('row4')" style="cursor:pointer;" ><td><input type="checkbox" name="row[]" id="row4"></td><td>冬天</td><td>晴</td></tr>
    </table>
      

  2.   

    你的代码中其实他给的JS没有错,你的输出有问题
    <tr style="cursor:pointer;" name = "tr" id="tr1" onclick="test(this)">
    <td><input type="checkbox" name="cb1" id="'.$print['user_id'].'"></td>
    这两句中的ID都是定值,都是tr1和cb1,而JS中的是动态的,要实现你的功能,这里应该写成’tr‘+i和‘cb’+i,i为行数