如何用js实现在网页中选中一个复选框,所有的复选框都被选中。
js复选框的问题
- 提问者网友:树红树绿
- 2021-04-16 02:46
- 五星知识达人网友:底特律间谍
- 2021-04-16 03:41
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>我爱周杰伦 </TITLE>
<script type="text/javascript" language="javascript">
//全选、全不选
function SelectAll(obj){
//获取所有的input的元素
var inputs = document.getElementsByTagName("input");
for(var i=0;i<inputs.length;i++){
//如果是复选框
if(inputs[i].type == "checkbox"){
inputs[i].checked = obj.checked;
}
}
}
</script>
</HEAD>
<BODY>
<input type="checkbox" onclick="SelectAll(this)">全选/全不选
<hr width="250px" align="left"/>
<input type="checkbox" />看书<br/>
<input type="checkbox" />听音乐<br/>
<input type="checkbox" />打篮球<br/>
<input type="checkbox" />编程<br/>
<hr width="250px" align="left"/>
</BODY>
</HTML>
- 1楼网友:夜余生
- 2021-04-16 04:23
<%@LANGUAGE="JAVASCRIPT" CODEPAGE="936"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns=" http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>实现全选全不选</title> <style type="text/css">
.fontColor{ font-size:17px; font-family:"华文行楷"; color:#7516D3 } .Color{ font-size:19px; font-family:"华文行楷"; color:#0000FF } </style>
<script type="text/javascript" language="javascript"> function choice(){ var btn = document.getElementById("btn"); var selected = document.getElementsByName("coffee"); if(btn.value == "都喜欢"){ btn.value = "都不喜欢"; for(var i=0; i<selected.length; i++){ if(selected[i].type=="checkbox"){ selected[i].checked = true; } } } else{ btn.value = "都喜欢"; for(var i=0; i<selected.length; i++){ if(selected[i].type=="checkbox"){ selected[i].checked = false; } } } } </script> </head>
<body> <p class="Color">你喜欢哪种类型的咖啡</p> <form id="Myform" name="form1" method="post" action="" class="fontColor"> <input type="checkbox" name="coffee" value="cream" />蓝山咖啡<br /> <input type="checkbox" name="coffee" value="sugar"/>摩卡<br /> <input type="checkbox" name="coffee" value="sugar"/>拿铁<br /> <input type="checkbox" name="coffee" value="sugar"/>卡布基诺<br /> <input type="checkbox" name="coffee" value="sugar"/>爱尔兰咖啡<br /> <input type="button" id="btn" value="都喜欢" class="onMouseOut" onclick="choice()" onmouseover="this.className = 'onMouseOver'" onmouseout="this.className = 'onMouseOut'"/> </form ></body> </html>
可以参考一下