Use Jquery/javascript to determine and change checkbox check Status

Source: Internet
Author: User

First, use jquery to judge and change the checkbox selected state

1. Use jquery to determine if a checkbox is selected:

(1). attr (' checked)

See jquery version 1.6+ return: "Checked" or "undefined"; 1.5-return: TRUE or False

(2). Prop (' checked ')

1.6+:true/false

(3). Is (': Checked ')

eg:$ ("input[type= ' checkbox ']"). Is (': Checked '), checked to True, not selected as false;

All versions: True/false

2. Change the selected state

(1) Use the Attr method to change the checkbox state.

Check: $ ("input[type= ' checkbox ']"). attr ("Checked", true);

Uncheck: $ ("input[type= ' checkbox ']"). attr ("checked", false);

(2) But sometimes the attr action is used, although the property value changes, but the checkbox on the page is not selected. In this case, it is recommended to use the Prop method to manipulate the checkbox selection state.

Selected:

$ ("input[type= ' checkbox ']"). Prop ("checked", true);

$ ("input[type= ' checkbox ']"). Prop ({checked:true}); Map Key value pairs

$ ("input[type= ' checkbox ']"). Prop ("Checked", function () {return true;}); /function returns TRUE or FALSE

$ ("input[type= ' checkbox ']"). Prop ("Checked", "checked");

Uncheck:

$ ("input[type= ' checkbox ']"). Prop ("checked", false);

3. Get Value values

Use jquery to get the value of the checkbox, and in the case where the value is not given, the value gets "on".

When using jquery to get the value of the checkbox, given the value, the obtained value is the value.

4. Example: Implementing a reverse selection

$ (": CheckBox"). each (function() {  if($ (). Prop ("checked")) {$ (   this). Prop ("Checked",false);  }Else{$ (this). Prop ("Checked",true); }});

which
Select All: $ (": CheckBox"). Prop ("checked", true)
Deselect all: $ (": CheckBox"). Prop ("checked", false)
Gets the selected: $ (": checkbox:checked")


Second, use JavaScript to determine and change the CheckBox check state

Judge:

document.getElementById ("Somecheckbox"). Checked//value is TRUE or False

Change

document.getElementById ("Somecheckbox"). Checked = true;

document.getElementById ("Somecheckbox"). Checked = false;

Use Jquery/javascript to determine and change checkbox check Status

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.