jquery implements a checkbox to select all, reverse.

Source: Internet
Author: User

1. When a select checkbox is selected, all sub-checkboxes are selected. Conversely, all do not choose

2. In the Sub-checkbox, deselect the Select CheckBox as long as there is no selection

3. If the number of sub-checkboxes is the same as the number of sub-checkboxes selected, the Select checkbox is selected

[JavaScript]View PlainCopy
  1. check box events
  2. Select All, Cancel all selected events
  3. function SelectAll () {
  4. if ($ ("#SelectAll"). attr ("checked")) {
  5. $ ("input[name= ' Subcheck ')"). attr ("Checked", true);
  6. } Else {
  7. $ (": CheckBox"). attr ("checked", false);
  8. }
  9. }
  10. Sub-check box events
  11. function Setselectall () {
  12. //When a child check box is not selected, SelectAll uncheck
  13. if (!$ ("#subcheck"). Checked) {
  14. $ ("#SelectAll"). attr ("Checked", false);
  15. }
  16. var chsub = $ ("input[type= ' checkbox '][name= ' Subcheck ']"). Length; //Get the number of Subcheck
  17. var checkedsub = $ ("input[type= ' checkbox '][name= ' Subcheck ']:checked"). Length; //Get the number of selected Subcheck
  18. if (checkedsub = = chsub) {
  19. $ ("#SelectAll"). attr ("Checked", true);
  20. } else{
  21. $ ("#SelectAll"). attr ("checked", false);}
  22. }

The ID of the Select checkbox is SelectAll, and the name of the sub-checkbox is Subcheck.

The JSP page code is as follows:

[HTML]View PlainCopy
  1. <input type="checkbox" id="SelectAll" value="Select All" onclick="SelectAll ();" />
  2. <input type="checkbox" name="subcheck[]" value="1" onclick="Setselectall ();" />
  3. <input type="checkbox" Name="subcheck[]" value="2" onclick=" Setselectall (); " />
  4. <input type="checkbox" Name="subcheck[]" value="3" onclick="Setselectall ();" />
  5. <input type="checkbox" Name="subcheck[]" value="4" onclick=" Setselectall (); " />

jquery implements a checkbox to select all, reverse.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.