<script language="javascript">
function selectCheckbox(name,value) { 
var checkObject = document.getElementsByName(name); 
value="1002,1003,"
var values = value.split(","); 
for(var j = 0; j < values.length; j++) 

for (var i = 0; i < checkObject.length; i++) 

if(checkObject[i].value == values[j]) 

checkObject[i].checked = true; 
break; 



}
 </script>
<body onload="selectCheckbox()">
<form id="form1" name="form1" method="post" action=""> 
<input type="checkbox" name="1001" id="1001" value="1001" /> <br> 
<input type="checkbox" name="1002" id="1002" value="1002" /> <br> 
<input type="checkbox" name="1003" id="1003" value="1003" /> <br> 
<input type="checkbox" name="1004" id="1004" value="1004" /> <br> </form>
</body>刷新页面,没有选中!请大家帮忙指点一下!谢谢!

解决方案 »

  1.   

    <body onload="selectCheckbox()">怎么没用参数?
    <form id="form1" name="form1" method="post" action=""> 
    <input type="checkbox" name="1001" id="1001" value="1001" /> <br> 
    <input type="checkbox" name="1002" id="1002" value="1002" /> <br> 
    <input type="checkbox" name="1003" id="1003" value="1003" /> <br> 
    <input type="checkbox" name="1004" id="1004" value="1004" /> <br> </form>
    </body>
      

  2.   

    还有你的<input type="checkbox" name="1004" id="1004" value="1004" /> name不对  应该都是一样的
      

  3.   

    html5有新的autofocus属性
    js有focus()方法
      

  4.   

    我将name改成同名,加了参数,还是不行啊
      

  5.   

     好吧我错了 居然把checkbox 看成textbox了 丢银啊function selectCheckbox () {
    document.getElementById("1001").setAttribute("checked","true");
    }楼主用这个方法吧 我试过可以
      

  6.   

    <script language="javascript">
    var str = "1002,1003,"; //从后台获取的字符串,豆号分割成数组!
    var arr=str.split(',');
    window.onload=function(){
    var input=document.getElementsByTagName("input");
    for(var j=0;j<input.length;j++)
     {if(input[j].type=="checkbox")
       {
       for(var i=0;i<arr.length;i++)
          {
          
          if(input[j].id==arr[i])
            {
             input[j].checked=true;
            }
          }
       }
     }
    }
    </script>