Operation Checkbox label and checkbox label

Source: Internet
Author: User

Operation Checkbox label and checkbox label

In front-end development, Checkbox operations are indispensable.

There are two common methods:. is () and. prop. The former is used to determine the status of the checkbox. The latter sets a value for the checkbox, which can be set to true or false.


Write a small example and practice:
In html:

<input id="Checkbox1" type="checkbox"  /><input id="Text1" type="text"   />

 

Assign a value to this text box. The value is the checkbox value. The only difference is whether the user selects the checkbox or not.



The above code, if you do not want to use if, can refer to the following jQuery code to modify, the final result is the same:

 

$ ('# Checkbox1 '). change (function () {var self = $ (this); // if (self. is (": checked") {// self. attr ("checked", true); // else {// self. attr ("checked", false); //} self. attr ("checked", self. is (": checked"); $ ('# text1 '). val (self. is (': checked '));});Source Code

 

Real-time Demonstration:

 

The following describes how to set a value for checkbox in The. prop () method.
The preceding example is called to determine whether or not to select the checkbox based on the value entered in the text box.
When the user inputs "true", the checkbox is the selected status. When the user inputs any non-"true" value or is not filled in, the checkbox status is not selected.

<input id="Text1" type="text" /><input id="Checkbox1" type="checkbox" />

 

Write jQuery code:

 

Demo:

 

Related Article

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.