Work case 1 everything is caused by check, case check

Source: Internet
Author: User

Work case 1 everything is caused by check, case check

In HTML, the input tag has two types: radio and checkbox. One single choice button and one check button. Jquery can select them through $ (": radio") and $ (": checkbox"). If you want to select the selected button, jquery can use $ (: checked).

Jquery judges three methods of checked (reposted content ):

 

. Attr ('checked'): // check version 1.6 + and return: "checked" or "undefined". 1.5-return: true or false
. Prop ('checked'): // 16 +: true/false
. Is (': checked'): // All Versions: true/false // do not forget the colon.

Several methods of jquery value assignment checked:

All jquery versions can be assigned the following values:

// $ ("# Cb1"). attr ("checked", "checked ");
// $ ("# Cb1"). attr ("checked", true );

Four values of jquery1.6 +: prop:

// $ ("# Cb1"). prop ("checked", true); // you can leave it simple.
// $ ("# Cb1"). prop ({checked: true}); // map key-Value Pair
// $ ("# Cb1"). prop ("checked", function (){
Return true; // The function returns true or false.
});

// Remember this: $ ("# cb1"). prop ("checked", "checked ");

 

Problem: multiple choice invert Selection

How to Use the selector mentioned above to complete the multiple-choice and inverse-selection functions. The single-choice and check buttons can be triggered by the click () event or the change () event.

There are several methods to change the selected or selected status:

Jquer Method

Attr () method and prop () method (reproduced)

When selecting all the check boxes, a problem occurs. Use the statement $. attr ('checked', true): Change the attribute of the check box to be selected. The first time you click "valid" in chrome, it will not work. IE8 is correct.

Baidu has been searching for a long time because HTML attributes are divided into attribute and property. The latter is called a feature for the moment.

The checked attribute is divided into attribute-> checked, and property-> true, false.

For a checkbox, if checked = "checked" is not defined, the result of alert ($. attr ("checked") is undefined. If defined, the result is checked. Attribute does not change with the status of the checkbox.

If prop ($. attr ("checked") is used, the output values are false and true, respectively. Property changes as it changes.

Therefore, you must use prop () to modify the checked attribute (). Prop () is added after jQuery1.6.

 

The following table is recommended for modifying attributes.

Javascript Method

CheckboxObject. checked = true | false

 

The following is a simple code for implementing the Single-choice and inverse-selection functions.

1 

 

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.