在网上看了不少的教程关于checkbox的,按照上面的做了,但是没有成功,于是自己下午找了一点时间来做这些方面的工作,要让复选框被选中其实很简单,就是给该元素加上checked属性,那么元素自然就会被选择上。要取消的话其实就是remove去他的这个熟悉。检测代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>checkbox</title>
<meta name="description" content="">
<meta name="keywords" content="">
<link href="" rel="stylesheet">
</head>
<body>
<form>
<input type="checkbox" >选中
<input type="checkbox" >选中
<input type="checkbox" >选中
<input type="checkbox" >选中
<div class="btn" id="btn">全选</div>
<div class="btn" id="cancer">取消</div>
</form>
<script type="text/javascript">
var btn=document.getElementById('btn');
var inputs=document.getElementsByTagName('input');
btn.onclick=function (argument) {
for (var i = inputs.length - 1; i >= 0; i--) {
inputs[i].setAttribute("checked",true);
};
alert(inputs[1].getAttribute("checked"));
}
var cancer=document.getElementById('cancer');
cancer.onclick=function (argument) {
for (var i = inputs.length - 1; i >= 0; i--) {
inputs[i].removeAttribute("checked");
};
alert(inputs[1].getAttribute("checked"));
}
</script>
</body>
</html>