<!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>
</head><body>
<input type="checkbox" id="c1" value="c1" />c1
<input type="checkbox" id="c2" value="c2" />c2
<input type="checkbox" id="c3" value="c3" />c3
<input type="checkbox" id="c4" value="c4" />c4
<input type="checkbox" id="c5" value="c5" />c5
<input type="checkbox" id="c6" value="c6" />c6
<input type="checkbox" id="c7" value="c7" />c7
<input type="checkbox" id="c8" value="c8" />c8
<input type="checkbox" id="c9" value="c9" />c9
<input type="checkbox" id="c10" value="c10" />c10
<input type="button" value="test" onclick="xlh()" /><script type="text/javascript">
var flag = 0;
var arr = new Array();
var xlh = function(){
for(var i=0;i<9;i++){
if(eval("c"+(i+1)).checked){
flag += 1;
arr.push("c"+(i+1));
}
}
if(flag !=0 && flag <4){
alert(arr.join("."));
}else{
alert("您选择超过了3条 或者 您根本就没选择");
}
}
</script>
</body>
</html>

解决方案 »

  1.   

    有点失误,应该把flag和arr的定义放到xlh()函数里面<script type="text/javascript">
    var xlh = function(){
    var flag = 0;
    var arr = new Array();
    for(var i=0;i<9;i++){
    if(eval("c"+(i+1)).checked){
    flag += 1;
    arr.push("c"+(i+1));
    }
    }
    if(flag !=0 && flag <4){
    alert(arr.join("."));
    }else{
    alert("您选择超过了3条 或者 您根本就没选择");
    }
    }
    </script>
      

  2.   

    不好意思哈,今天写代码总是不严谨,上面的没有在firefox测试,经测试不正常
    所以又改了一次,下面的可以在不同的流行浏览器得到支持吧:<!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">
    var xlh = function(){
    var flag = 0;
    var arr = new Array();
    for(var i=0;i<10;i++){
    if(document.getElementById("c"+(i+1)).checked){
    flag += 1;
    arr.push("c"+(i+1));
    }
    }
    if(flag !=0 && flag <4){
    alert(arr.join("."));
    }else{
    alert("您选择超过了3条 或者 您根本就没选择");
    }
    }
    </script>
    </head><body>
    <input type="checkbox" id="c1" value="c1" />c1
    <input type="checkbox" id="c2" value="c2" />c2
    <input type="checkbox" id="c3" value="c3" />c3
    <input type="checkbox" id="c4" value="c4" />c4
    <input type="checkbox" id="c5" value="c5" />c5
    <input type="checkbox" id="c6" value="c6" />c6
    <input type="checkbox" id="c7" value="c7" />c7
    <input type="checkbox" id="c8" value="c8" />c8
    <input type="checkbox" id="c9" value="c9" />c9
    <input type="checkbox" id="c10" value="c10" />c10
    <input type="button" value="test" onclick="xlh()" /></body>
    </html>
      

  3.   

    楼上的你为什么要使用charset=utf-8 编码呢?我的IE上都显示的乱码啊
    改成charset=GBK就显示正常了
      

  4.   

    这样感觉不好,能不能根据object取得他的id 呢?