<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>
时间: 2024-10-07 22:35:59