node2:/var/www/html#cat a17.html
<input type="checkbox" value="1" name="check" checked/>
<input type="checkbox" value="2" name="check"/>
<input type="checkbox" value="3" name="check" checked/>
<input type="button" value="你选中的个数" id="btn"/>
<script type="text/javascript" src="jquery-2.2.2.min.js"></script>
<script type="text/javascript" src="a17.js"></script>
node2:/var/www/html#cat a17.js
var items = document.getElementsByName("check"); //获取id为btn的元素(button)
for (var i=0;i<items.length;i++){ //由于获取的是数组对象,因此需要把它循环出来
console.log(items[i]);
}
node2:/var/www/html#
输出:
导航至 http://192.168.137.3/a17.html
<input value="1" name="check" checked="" type="checkbox">
a17.js:3:7
<input value="2" name="check" type="checkbox">
a17.js:3:7
<input value="3" name="check" checked="" type="checkbox">