jquery实现全选,反选,不选和提交按钮

来源:互联网 发布:java shell 交互 编辑:程序博客网 时间:2024/05/06 08:05

我的一个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);
}