js实现简单的全选和反选,一般都用得上。代码很简单,就这一小段。
顺便把完整的测试代码贴出来
本文链接:https://liuyanzhao.com/6042.html
- function DoCheck() {
- var ch = document.getElementsByName("ids");
- if (document.getElementById("allSelect").checked == true) {
- for (var i = 0; i < ch.length; i++) {
- ch[i].checked = true;
- }
- } else {
- for (var i = 0; i < ch.length; i++) {
- ch[i].checked = false;
- }
- }
- }
顺便把完整的测试代码贴出来
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>全选和反选演示</title>
- <script type="text/javascript">
- function DoCheck() {
- var ch = document.getElementsByName("ids");
- if (document.getElementById("allSelect").checked == true) {
- for (var i = 0; i < ch.length; i++) {
- ch[i].checked = true;
- }
- } else {
- for (var i = 0; i < ch.length; i++) {
- ch[i].checked = false;
- }
- }
- }
- </script>
- </head>
- <body>
- <table border="1px solid #ccc">
- <tr>
- <td>
- <input type="checkbox" id="allSelect" onclick="javascript:DoCheck()">
- </td>
- <td>姓名</td>
- <td>性别</td>
- <td>年龄</td>
- <td>职业</td>
- </tr>
- <tr>
- <td>
- <input type="checkbox" name="ids">
- </td>
- <td>李白</td>
- <td>男</td>
- <td>28</td>
- <td>诗人</td>
- </tr>
- <tr>
- <td>
- <input type="checkbox" name="ids">
- </td>
- <td>周瑜</td>
- <td>男</td>
- <td>19</td>
- <td>智将</td>
- </tr>
- <tr>
- <td>
- <input type="checkbox" name="ids">
- </td>
- <td>杨玉环</td>
- <td>女</td>
- <td>25</td>
- <td>贵妃</td>
- </tr>
- </table>
- </body>
- </html>
本文链接:https://liuyanzhao.com/6042.html
您可以选择一种方式赞助本站
支付宝扫一扫赞助
微信钱包扫描赞助
赏