日期:2014-05-16  浏览次数:20350 次

一个简单的全选问题
HTML code

<!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 type="text/javascript">
    function checked(){
    var all=document.getElementsByName("one");
    for(var i=0;i<all.length;i++){
        if(document.getElementById("all").checked==true)
                {  
                all[i].checked=true;
                }
        else    {
                all[i].checked=false;
                }
        }
    }

</script>
</head>

<body>
全选<input id="all" type="checkbox" onclick="checked();"/>
  <p> <input name="one" type="checkbox" /> </p>
  <p><input name="one" type="checkbox" /> </p> 
  <p><input name="one" type="checkbox" /> </p>
</body>
</html>



没法正确全选 用fireBug也看不出错误
IE提示 对象不支持此操作
请问是怎么回事

------解决方案--------------------
换个函数名
<!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 type="text/javascript">
function checked1(){
var all=document.getElementsByName("one");
for(var i=0;i<all.length;i++){
if(document.getElementById("all").checked==true)
{
all[i].checked=true;
}
else {
all[i].checked=false;
}
}
}

</script>
</head>

<body>
全选<input id="all" type="checkbox" onclick="checked1();"/>
<p> <input name="one" type="checkbox" /> </p>
<p><input name="one" type="checkbox" /> </p> 
<p><input name="one" type="checkbox" /> </p>
</body>
</html>