HTML一个按钮实现全选反选功能,大神们帮我挑错

做好说说错在哪里,需要怎么改<title>全选反选:有问题</title><scriptlanguage="javascript">functionsellectAll(... 做好说说错在哪里,需要怎么改

<title>全选反选:有问题</title>
<script language="javascript">
function sellectAll(){
var arr = document.getElementsByName("like");
var b = document.getElementById("btu");
if(b=="全选"){
for(int i=0;i<arr.length;i++){
var c = arr[i];
c.checked=true;
}
document.getElementById("btu").value="反选";
}
else {
for(int i=0;i<arr.length;i++){
var c = arr[i];
c.checked=false;
}
document.getElementById("btu").value="全选";
}
}
</script>
</head>

<body>
<form>
<input type="checkbox" value="读书" name="like" />读书<br />
<input type="checkbox" value="看书" name="like" />看书<br />
<input type="checkbox" value="写书" name="like" />写书<br />
<input type="checkbox" value="听书" name="like" />听书<br />
<input type="button" onclick="sellectAll()" id="btu" value="全选" />
</form>
</body>
</html>
展开
 我来答
闯95
2014-12-09 · TA获得超过259个赞
知道小有建树答主
回答量:80
采纳率:100%
帮助的人:99.4万
展开全部
<!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=utf-8" />
<title>无标题文档</title>
<script language="javascript">
window.onload=function()
{
var arr =document.getElementsByTagName('input');
var b = document.getElementById("btu");
var iSelect=true;
b.onclick=function()
{
if(iSelect==true)
{
for(var i=0;i<arr.length;i++)
{
arr[i].checked=true;
}
iSelect=false;
b.value='反选';
}
else if(iSelect==false)
{
for(var i=0;i<arr.length;i++)
{
arr[i].checked=false;
}
iSelect=true;
b.value='全选';
}

}
}
</script>
</head>

<body>
<form>
<input type="checkbox" value="读书" name="like" />读书<br />
<input type="checkbox" value="看书" name="like" />看书<br />
<input type="checkbox" value="写书" name="like" />写书<br />
<input type="checkbox" value="听书" name="like" />听书<br />
<input type="button" id="btu" value="全选" />
</form>
</body>
</html>

你错的有点不合理啊,首先js是类c语言,但是一种弱语言,定义变量是用var 你用的是int。其次var b=document.getElementById("btu"); b是你选取的元素对象,你怎么就可以直接用来判断(b=="全选")这是不对的。我在你的基础上改好了,你自己好好看看吧
追问
我的老师就是这个思路做出来的,我顺着她的思路自己做,就没做出来,我是新手,你的代码太高深,iSelect都不懂……
追答
我的代码不高深啊,都是简单的基础,iSelect是个全局变量,用来记录此时到底是全选还是反选,一开始都没有被选中此时iSelect=true,当全选后iSelect=false,这样就会有了判断的条件
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式