JAVASCRIPT实现翻页保存已勾选的项目

<input type="checkbox" name="a" value="1" /><br/>
<input type="checkbox" name="a" value="2" /><br/>
<input type="checkbox" name="a" value="3" /><br/>
<input type="checkbox" name="a" value="4" /><br/>

<input type="button" onclick="changeIds()" />

<script type="text/javascript">
    checkedIds="";//翻页保存选中的id
      
          function changeIds(){
               var oneches=document.getElementsByName("a");
             for(var i=0;i<oneches.length;i++){
                 if(oneches[i].checked==true){
                     //避免重复累计id (不含该id时进行累加)
                     if(checkedIds.indexOf(oneches[i].value)==-1){
                         checkedIds=checkedIds+oneches[i].value+",";
                     }
                 }
                 if(oneches[i].checked==false){
                     //取消复选框时 含有该id时将id从全局变量中去除
                     if(checkedIds.indexOf(oneches[i].value)!=-1){
                         checkedIds=checkedIds.replace((oneches[i].value+","),"");
                     }
                 }
             }
             alert(checkedIds);
          }
     
    function getChecked(){
        checkedIds = '4,1,';
                var oneches=document.getElementsByName("a");
             for(var i=0;i<oneches.length;i++){
                     //全局变量中含有id,则该复选框选中
                     if(checkedIds.indexOf(oneches[i].value)!=-1){
                         oneches[i].checked=true;
                     }
                 }

                 alert(checkedIds);
          }
 
getChecked();
</script>

  

posted @ 2015-07-24 16:14  Adtuu  阅读(667)  评论(0编辑  收藏  举报