When you click to select all or invert the selection, only the first one will change, and the others will not be reflected
<code><?php include("../../admin/public/header.php"); include("../../admin/public/admin_left.php"); $sql="select * from ts_users "; $res=mysql_query($sql); $arr=array(); if(mysql_num_rows($res)>0){ while(($list=mysql_fetch_assoc($res))!=false){ $arr[]=$list; } } ?> <div id="div"> <table border=0 cellspacing=0 width="900px"> <tr height="60px"> <th>选择</th> <th>用户名</th> <th>邮箱</th> <th>手机</th> <th>创建事件</th> <th>最后登陆时间</th> <th>操作</th> </tr> <?php if(isset($arr)) { foreach ($arr as $vo) { ?> <tr align="center" height="30px"> <td id="td"><input type="checkbox" value="<?=$vo['uid']?>" ></td> <td><?= $vo['uname'] ?></td> <td><?= $vo['email'] ?></td> <td><?= $vo['phone'] ?></td> <td><?= $vo['ctime'] ?></td> <td><?= $vo['mtime'] ?></td> <td> <a href="./delete_uid.php?uid=<?= $vo['uid'] ?>">删除</a> <a href="./update_uid.php?uid=<?= $vo['uid'] ?>">编辑</a> </td> </tr> <?php } } ?> </table> <input type="button" value="全选" onclick="allcheck()"> <input type="button" value="反选" onclick="check()"> </div> <script> var div=document.getElementById("td"); var childs=div.childNodes; function allcheck(){ for(var i=0;i<childs.length;i++){ if(childs[i].nodeName=="INPUT" && childs[i].getAttribute('type')=='checkbox'){ childs[i].checked=true; } } } function check(){ for(var i=0;i<childs.length;i++){ if(childs[i].nodeName=="INPUT" && childs[i].getAttribute('type')=='checkbox'){ if(childs[i].checked==true){ childs[i].checked=false; }else{ childs[i].checked=true; } } } } </script> </body> </html></code>
When you click to select all or invert the selection, only the first one will change, and the others will not be reflected
<code><?php include("../../admin/public/header.php"); include("../../admin/public/admin_left.php"); $sql="select * from ts_users "; $res=mysql_query($sql); $arr=array(); if(mysql_num_rows($res)>0){ while(($list=mysql_fetch_assoc($res))!=false){ $arr[]=$list; } } ?> <div id="div"> <table border=0 cellspacing=0 width="900px"> <tr height="60px"> <th>选择</th> <th>用户名</th> <th>邮箱</th> <th>手机</th> <th>创建事件</th> <th>最后登陆时间</th> <th>操作</th> </tr> <?php if(isset($arr)) { foreach ($arr as $vo) { ?> <tr align="center" height="30px"> <td id="td"><input type="checkbox" value="<?=$vo['uid']?>" ></td> <td><?= $vo['uname'] ?></td> <td><?= $vo['email'] ?></td> <td><?= $vo['phone'] ?></td> <td><?= $vo['ctime'] ?></td> <td><?= $vo['mtime'] ?></td> <td> <a href="./delete_uid.php?uid=<?= $vo['uid'] ?>">删除</a> <a href="./update_uid.php?uid=<?= $vo['uid'] ?>">编辑</a> </td> </tr> <?php } } ?> </table> <input type="button" value="全选" onclick="allcheck()"> <input type="button" value="反选" onclick="check()"> </div> <script> var div=document.getElementById("td"); var childs=div.childNodes; function allcheck(){ for(var i=0;i<childs.length;i++){ if(childs[i].nodeName=="INPUT" && childs[i].getAttribute('type')=='checkbox'){ childs[i].checked=true; } } } function check(){ for(var i=0;i<childs.length;i++){ if(childs[i].nodeName=="INPUT" && childs[i].getAttribute('type')=='checkbox'){ if(childs[i].checked==true){ childs[i].checked=false; }else{ childs[i].checked=true; } } } } </script> </body> </html></code>
There can only be one
id
in the entire document, use class
<code><td class="td"><input type="checkbox" value="<?=$vo['uid']?>" ></td></code>
Replace id='td' with class='td'