1. 程式人生 > >jquery實現全選,反選,不選和提交按鈕

jquery實現全選,反選,不選和提交按鈕

我的一個php檔案:

<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="check.js"></script>
</head>
<body>
<form action="" method="get" id="myFrom">
<input type="checkbox" name="checkbox" value="1">checkbox1<br/>
<input type="checkbox" name="checkbox" value="2">checkbox2<br/>
<input type="checkbox" name="checkbox" value="3">checkbox3<br/>
<input type="checkbox" name="checkbox" value="4">checkbox4<br/>
<input type="checkbox" name="checkbox" value="5">checkbox5<br/>
<input type="checkbox" name="checkbox" value="6">checkbox6<br/>
選擇:<a id="quanxuan" href="#">全選</a>&nbsp;<a id="fanxuan" href="#">反選</a>&nbsp;<a id="buxuan" href="#">不選</a><br/>
<input type="button" value="提交" id="submit">
</form>
</body>
</html>

我的check.js檔案

$(document).ready(function(){
 $('#myFrom').find("#quanxuan").bind("click",quanxuan);
 $('#myFrom').find("#fanxuan").bind("click",fanxuan);
 $('#myFrom').find("#buxuan").bind("click",buxuan);
 $("#myFrom").find("#submit").bind("click",submit);
});

function quanxuan(){
 $('#myFrom').find("[name='checkbox']").attr("checked",true);
}
function fanxuan(){
 $('#myFrom').find("[name='checkbox']").each(function(){
  if($(this).attr("checked")){
   $(this).removeAttr("checked");
  }else{
   $(this).attr("checked",true);
  }
 });
}
function buxuan(){
 $('#myFrom').find("[name='checkbox']").each(function(){
  if($(this).attr("checked")){
   $(this).removeAttr("checked");
  }
 });
}
function submit(){
 var str="";
 $('#myFrom').find("[name='checkbox']").each(function(){
  if($(this).attr("checked")){
   str+=$(this).val()+" "
  }
 });
 alert(str);
}