代码跟抽风似得,一会好使 一会不好使 这样写全选和全不选都不好使了 请大家帮看看 谢谢啦
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>无标题文档</title>
</head>
<body>
<form>
请选择你爱好:<br>
<input type="checkbox" name="hobby" id="hobby1"> 音乐
<input type="checkbox" name="hobby" id="hobby2"> 登山
<input type="checkbox" name="hobby" id="hobby3"> 游泳
<input type="checkbox" name="hobby" id="hobby4"> 阅读
<input type="checkbox" name="hobby" id="hobby5"> 打球
<input type="checkbox" name="hobby" id="hobby6"> 跑步 <br>
<input type="button" value = "全选" onclick = "checkall()">
<input type="button" value = "全不选" onclick = "clearall()">
<p>请输入您要选择爱好的序号,序号为1-6:</p>
<input id="wb" name="wb" type="text" >
<input name="ok" type="button" value="确定" onclick = "checkone()">
</form>
<script type="text/javascript">
//全选
function checkall(){
for (var i = 0; i < inputs.length; i++) {
var inputs = document.getElementsByTagName("input");
if (inputs.type = "checkbox") {
inputs[i].checked = true;
}
}
}
//全不选
function clearall(){
var hobbys = document.getElementsByName("hobby");
for (var i = 0; i < hobby.length; i++) {
hobbys[i].checked = false;
}
}
//单选函数
function checkone(){
var j=document.getElementById("wb").value;
switch(parseInt(j)){
case 1:
var hobby1 = document.getElementById("hobby1");
hobby1.checked = true;
break;
case 2:
var hobby2 = document.getElementById("hobby2");
hobby2.checked = true;
break;
case 3:
var hobby3 = document.getElementById("hobby3");
hobby3.checked = true;
break;
case 4:
var hobby4 = document.getElementById("hobby4");
hobby4.checked = true;
break;
case 5:
var hobby5 = document.getElementById("hobby5");
hobby5.checked = true;
break;
case 6:
var hobby6 = document.getElementById("hobby6");
hobby6.checked = true;
break;
}
}
</script>
</body>
</html>