通过DOM获取具有相同name值的input 完成全选操作

Original 2019-03-26 00:23:14 250
abstract:<!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8">     <title>Title</title&g
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">
 .big{width: 200px;height: auto;border: 1px solid #000;margin: 20px auto;text-align: center}
        .big input{margin: 10px;}
    </style>
    <script type="text/javascript">
 function checkAll() {
            var all = document.getElementById('first_in');//获取
 var one = document.getElementsByName('item');//通过获取name对应的input元素,可得到input的数量(前提是name值全都一样)
 var x = 0;//getElementsByName获取到的是数组,所以要加[];x充当下标
 while (x < one.length){
                if (all.checked){
                    one[x].checked = true;
 } else {
                    one[x].checked = false;
 }
                x++;
 }
        }
    </script>
</head>
<body>
    <div class="big">

        <input type="checkbox" id="first_in" onclick="checkAll()" lick><lable for="first_in">全选</lable><hr>

        <input type="checkbox" name="item">选项1<hr>
        <input type="checkbox" name="item">选项2<hr>
        <input type="checkbox" name="item">选项3<hr>
        <input type="checkbox" name="item">选项4<hr>
        <input type="checkbox" name="item">选项5<hr>
        <input type="checkbox" name="item">选项6<hr>
        <input type="checkbox" name="item">选项7<hr>
        <input type="checkbox" name="item">选项8
    </div>
</body>
</html>


Correcting teacher:查无此人Correction time:2019-03-26 09:12:58
Teacher's summary:完成的不错。js的标签获取,需要多练习,这个很重要,继续加油。

Release Notes

Popular Entries