日期:2014-05-17  浏览次数:20462 次

这样的JS效果怎么做出来?
一个表单中有
<input   type= "checkbox "   name= "delete[] "   />
列表,数目未知
我想让用户点下某个按钮就把选中的取消,没选中的就选中,怎么弄呢?

------解决方案--------------------
<script language=javascript>
function btnCheck_onclick(){
var mycols=document.getElementsByName( "chk ");
for (var i=0;i <mycols.length;i++){
if (mycols[i].checked==true){
mycols[i].checked=false;
} else {
mycols[i].checked=true;
}
}
}
</script>
<input type=checkbox name=chk value= "1 ">
<input type=checkbox name=chk value= "2 ">
<input type=checkbox name=chk value= "3 ">
<input type=checkbox name=chk value= "4 ">
<input type=checkbox name=chk value= "5 ">
<input type=checkbox name=chk value= "6 ">
<input type=checkbox name=chk value= "7 ">
<input type=button value= " 测 试 " onclick= "btnCheck_onclick() ">

------解决方案--------------------
if (mycols[i].checked==true){
mycols[i].checked=false;
} else {
mycols[i].checked=true;
}

这段这么麻烦

mycols[i].checked = mycols[i].checked ? false : true;
------解决方案--------------------
<script language= "javascript ">
function selectIt(){
form=document.Form1
action=event.srcElement.name
for(var i=0 ;i <form.elements.length;i++){
if(form.elements[i].name== "checkbox "){
e=form.elements[i]
e.checked=(action== "selectAll ")?(form.selectAll.checked):(!e.checked)
}
}
}
// 全选
function allCheck()
{
for (var i=0;i <Form1.elements.length;i++)
{
var e=Form1.elements[i];
if (e.type== 'checkbox ')
e.checked=true;
}

}
//反选
function revCheck()
{
for (var i=0;i <Form1.elements.length;i++)
{
var e=Form1.elements[i];
if (e.type== 'checkbox ')
e.checked=!e.checked;
}
}
</script>
<form name= "Form1 ">
<p class= "STYLE1 ">
<button onclick= "revCheck() " type= "button "> 全部反选 </button> <br />
部分全选
<input type= "checkbox " name= "selectAll " value= "checkbox " onClick= "selectIt() "> <br>
部分反选
<input type= "checkbox " name= "invest " value= "checkbox " onClick= "selectIt() "> <br>
Item1
<input type= "checkbox " name= "checkbox " value= "checkbox ">
<br>
Item2
<input type= "checkbox " name= "checkbox " value= "checkbox "> <br>
Item3
<input type= "checkbox " name= "checkbox " value= "checkbox "> <br>
Item4
<input type= "checkbox " name= "checkbox " value= "checkbox "> <br>
Item5
<input type= "checkbox " name= "checkbox " value= "checkbox ">
</p>
</form>

两种方案