欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于復(fù)選框demo(分享)

 更新時(shí)間:2017年09月27日 08:37:38   作者:恭一  
下面小編就為大家?guī)?lái)一篇基于復(fù)選框demo(分享)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

本篇文章是關(guān)于復(fù)選框的,有2種形式:1、全選、反選由2個(gè)按鈕實(shí)現(xiàn);2、全選、反選由一個(gè)按鈕實(shí)現(xiàn)。

<!DOCTYPE html>
<html>
 <head>
  <meta charset="UTF-8">
  <title>復(fù)選框demo</title>
  <script src="../js/jquery-1.10.2.js" type="text/javascript"></script>
  <style>
   body{ text-align:center} 
   .con{ margin:100px auto; width:800px; height:400px; border:1px solid #F00; padding-top: 50px;} 
  </style> 
 </head>
 <body>
  <div class="con">
   <span><input type='checkbox' name='select' onclick='allSelect()'>全選</span>
   <span><input type='checkbox' name='cancel' onclick='unAllSelect()'>反選</span>
   <span><input type='checkbox' name='fruit' />蘋(píng)果</span>
   <span><input type='checkbox' name='fruit' />香蕉</span>
   <span><input type='checkbox' name='fruit' />梨子</span>
   <span><input type='checkbox' name='fruit' />桃子</span>
   <span><input type='checkbox' name='fruit' />西瓜</span>
   
   <br><br><br>
   
   <span><input type='checkbox' id="allBook" name='allBook' />全選</span>
   <span><input type='checkbox' name='book' />老子</span>
   <span><input type='checkbox' name='book' />尚書(shū)</span>
   <span><input type='checkbox' name='book' />周易</span>
   <span><input type='checkbox' name='book' />詩(shī)經(jīng)</span>
   <span><input type='checkbox' name='book' />孟子</span>
   <span><input type='checkbox' name='book' />中庸</span>
   
<script type="text/javascript">
 //全選
 function allSelect(){
  $("input[name='fruit']").prop("checked", "checked");
  $("input[name='cancel']").removeAttr("checked");
 }
 //反選
 function unAllSelect(){
  $("input[name='fruit']").removeAttr("checked");
  $("input[name='select']").removeAttr("checked");
 }
 //單選
 $("#allBook").click(function(){
  if(this.checked){
//   $("input[name='book']").attr("checked", true);
   $("input[name='book']").prop("checked", "checked");
   }else{
//   $("input[name='book']").attr("checked", false);
   $("input[name='book']").removeAttr("checked");
   }
 });
</script>

  </div>
 </body>
</html>

在實(shí)踐中碰到一個(gè)問(wèn)題——check全選失效。解決辦法,使用prop方法代替attr。

$("input[name='book']").attr("checked", "checked"); 
$("input[name='book']").prop("checked", "checked"); 

這或許是和jQuery版本有關(guān)。

以上這篇基于復(fù)選框demo(分享)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論