Jquery sets the readonly and disabled of elements (including the checkbox read-only method)

Source: Internet
Author: User

Jquery APIs provide the following methods to apply the disabled and readonly attributes to elements:

1. readonly

$ ('Input '). ATTR ("readonly", "readonly") // set the input element to readonly $ ('input '). removeattr ("readonly"); // remove the readonly attribute of the input element if ($ ('input '). ATTR ("readonly") = true) // determine whether the readonly attribute has been set for the input element. There are two methods to set the readonly attribute for the element and cancel the readonly attribute: $ ('input '). ATTR ("readonly", true) // set the input element to readonly $ ('input '). ATTR ("readonly", false) // remove the readonly attribute of the input element $ ('input '). ATTR ("readonly", "readonly") // set the input element to readonly $ ('input '). ATTR ("readonly", "") // remove the readonly attribute of the input element

2. Disabled
 

$ ('Input '). ATTR ("disabled", "disabled") // set the input element to disabled $ ('input '). removeattr ("disabled"); // remove the disabled attribute of the input element if ($ ('input '). ATTR ("disabled") = true) // you can determine whether the disabled attribute has been set for the input element. There are two methods to set the disabled attribute for the element and cancel the disabled attribute: $ ('input '). ATTR ("disabled", true) // set the input element to disabled $ ('input '). ATTR ("disabled", false) // remove the disabled attribute of the input element $ ('input '). ATTR ("disabled", "disabled") // set the input element to disabled $ ('input '). ATTR ("disabled", "") // remove the disabled attribute of the input element

3. About checkbox (check whether a group of checkbox values are determined by the same name)
Because the checkbox itself does not provide the readonly method, and disabled will become gray, so we found two methods.

 

Method 1: the checkbox does not have the readonly attribute. If the Disabled = "disabled" attribute is used, the checkbox will become gray. The user is disgusted with this style so that it can be read-only: set its onclick = "Return false" js to checkbox. onclick = function () {return false ;}; Method 2: <SCRIPT src = "demo3.1/jquery-1.4.2.min.js" type = "text/JavaScript"> </SCRIPT> <script language = "JavaScript"> $ (function () {$ ("input [type = 'checkbox']"). click (function () {This. checked =! This. Checked ;}); </SCRIPT>

4. Read-Only settings for select

We recommend that you use disabled instead of using the checkbox method.

 

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.