萬盛學電腦網

 萬盛學電腦網 >> 腳本專題 >> javascript >> jquery做復選框的全選、全部選、反選

jquery做復選框的全選、全部選、反選

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>復選框的全選反選全不選</title>
<style>
body{ margin:200px;}
</style>
<script language="javascript" type="text/javascript" src="../script/jquery-1.4.2.min.js"></script>
<script>
$(function(){
 $("input[value='全選']").click(function(){
   $("input[type=checkbox]").attr("checked",true);
  })
  $("input[value='全不選']").click(function(){
   $("input[type=checkbox]").attr("checked",false);
  })
  $("input[value='反選']").click(function(){
   $("input[type=checkbox]").each(function(){
   $(this).attr("checked",!$(this).attr("checked"));
    })
  })
 })
</script>
</head>
<body>
<div id="div">
<input type="checkbox" />蘋果
<input type="checkbox" />香蕉
<input type="checkbox" />菠蘿
<input type="checkbox" />草莓
<input type="checkbox" />梨子<br />
<input type="button" value="全選" /><input type="button" value="反選" /><input type="button" value="全不選" />
</div>
</body>
</html>
copyright © 萬盛學電腦網 all rights reserved