点击一个按纽,将选项全选。写了下总是不对,高手指点下.谢谢!
<html>
<title>choseFruit</title>
<script lanuage="javascript">
var len = document.form1.fruit.length;
function chooseAll()
{
for(var i=0; i<len; i++)
{
document.form1.fruit[i].checked = document.form1.fruits.checked;
}
}
</script>
<form action="" method=post name=form1>
<input type="checkbox" name=fruit value="0">苹果</br>
<input type="checkbox" name=fruit value="1">桔子</br>
<input type="checkbox" name=fruit value="2">香蕉</br>
<input type="checkbox" name=fruit value="3">葡萄</br>
<input type="checkbox" name=fruit value="4">桃子</br>
<input type="checkbox" name=fruits value="5" onclick="chooseAll()">全选</br>
</form>
<html>
<title>choseFruit</title>
<script lanuage="javascript">
var len = document.form1.fruit.length;
function chooseAll()
{
for(var i=0; i<len; i++)
{
document.form1.fruit[i].checked = document.form1.fruits.checked;
}
}
</script>
<form action="" method=post name=form1>
<input type="checkbox" name=fruit value="0">苹果</br>
<input type="checkbox" name=fruit value="1">桔子</br>
<input type="checkbox" name=fruit value="2">香蕉</br>
<input type="checkbox" name=fruit value="3">葡萄</br>
<input type="checkbox" name=fruit value="4">桃子</br>
<input type="checkbox" name=fruits value="5" onclick="chooseAll()">全选</br>
</form>
<head>
<title>choseFruit</title>
<script lanuage="javascript">
var flag = false;
function chooseAll()
{
var len = document.form1.fruit.length;
alert(flag)
for(var i=0; i<len; i++)
{
document.form1.fruit[i].checked = (flag == false?true:false);
}
flag=(flag == false?true:false);
}
</script>
</head>
<body>
<form action="" method=post name=form1>
<input type="checkbox" name=fruit value="0">苹果</br>
<input type="checkbox" name=fruit value="1">桔子</br>
<input type="checkbox" name=fruit value="2">香蕉</br>
<input type="checkbox" name=fruit value="3">葡萄</br>
<input type="checkbox" name=fruit value="4">桃子</br>
<input type="checkbox" name=fruits value="5" onclick="chooseAll()">全选</br>
</form>
</body>
</html>
全选中,全反选....
1. lanuage ---> language
2. var len = document.form1.fruit.length; 不应该放在form的前面
办法:
把你的<script language="javascript"> ... </script> 放在</form>后就ok了!
<HTML>
<HEAD>
<TITLE> SELECT </TITLE><META NAME="Description" CONTENT="">
</HEAD>
<script language="javascript"> function selectAll(){
var sel = document.getElementById("selId");
var List = document.getElementsByName("fruit");
var len = List.length;
if(sel.checked==true){
for(var i = 0; i<len; i++){
List[i].checked = true;
}
}else{
for(var i = 0; i<len; i++){
List[i].checked = false;
}
}
}
function checkSel(){
var List = document.getElementsByName("fruit");
var len = List.length;
var j = 0;
for(var i = 0; i<len; i++){
if(List[i].checked == true){
j++;
}
}
if(j == len){
document.getElementById("selId").checked = true;
}else{
document.getElementById("selId").checked = false;
}
}
</script>
<BODY>
<form action="" method=post name=form1>
<input type="checkbox" name=fruit value="0" onclick="checkSel();">苹果</br>
<input type="checkbox" name=fruit value="1" onclick="checkSel();">桔子</br>
<input type="checkbox" name=fruit value="2" onclick="checkSel();">香蕉</br>
<input type="checkbox" name=fruit value="3" onclick="checkSel();">葡萄</br>
<input type="checkbox" name=fruit value="4" onclick="checkSel();">桃子</br>
</form>
<input type="checkbox" id="selId" onclick="selectAll();">全选/取消
</BODY>
</HTML>