javascript全选与全不选(求大神帮我改下)
<!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>
<script type="text/javascript" language="javascript">
function btnAll()
{
var ALL =document.getElementsByName("cbAihao");
var Btn = document.getElementById("btn").value;
if(Btn=="都喜欢")
{
for(i=0;i<aihao.length;i++){
aihao[i].checked=document.getElementById("btn").value = "都不喜欢";
// document.getElementById("btn").value = "都不喜欢";
}
}
else
{
document.getElementById("btn").value = "都喜欢";
}
}
</script>
</head>
<body>
<p>你喜欢喝哪一种咖啡呀</p>
<input type="checkbox" name="cbAihao">拿铁<br>
<input type="checkbox" name="cbAihao">蓝山咖啡<br>
<input type="checkbox" name="cbAihao">卡布奇洛<br>
<input type="checkbox" name="cbAihao">爱尔兰咖啡<br>
<input type="button" value="都喜欢" id="btn" onClick="btnAll()">
</body>
</html>
------解决方案--------------------function btnAll()
{
var ALL =document.getElementsByName("cbAihao");
var Btn = document.getElementById("btn").value;
var chkbool = Btn=="都喜欢"?true:false;
for(i=0;i<ALL.length;i++)
{
ALL[i].checked = chkbool;
}
document.getElementById("btn").value = chkbool?"都不喜欢":"都喜欢";
}