jquery實現複選框checkbox全選(完善)

來源:互聯網
上載者:User

實現:當一個未選中或者未全部選中時點擊全選進行全部選中,而再次點全選則取消全部選中。點擊反選則就進行反選。

html

<body><form>   <input type="checkbox" name="mycheck" value="樸樹" />樸樹   <input type="checkbox" name="mycheck" value="許巍" />許巍   <input type="checkbox" name="mycheck" value="鄭鈞" />鄭鈞   <input type="checkbox" name="mycheck" value="曹方" />曹方   <input type="checkbox" name="mycheck" value="張懸" />張懸<br>      <input type="button"  id="checkAll" value="全選" />   <input type="button"  id="checkRev" value="反選" /></form></body>

js

// JavaScript Document$(document).ready(function(){   //全選    $("#checkAll").click(function(){        //如果沒有全部被選中,則全選        if($('[name=mycheck]:checked').length!=$('[name=mycheck]').length){        $('[name=mycheck]').attr("checked",true);        //否則 全部取消選中         }else{        $('[name=mycheck]').attr("checked",false);        }    })   //反選    $("#checkRev").click(function(){       $('[name=mycheck]').each(function(){  //依次遍曆每個 checkbox       $(this).attr("checked",!$(this).attr("checked"));       })   }) })

 

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.