Use js to dynamically control the read-only attribute of the input box

Source: Internet
Author: User
Read-only attribute of the input box: When readonly is directly added as read-only on the page, you can directly add readonly & quot; in the input box ;, but if you want to change it by clicking the button, you need to implement it through js (or jQuery. Read-only attribute of the input box: readonly

When you directly add readonly = "readonly" to the page, you can directly add readonly = "readonly" to the input. To change the value by clicking the button, you must use js (or jquery).

The last time I used this, I finally found out why I was so depressed when I wrote this js control. Originally, when js set attributes such as readonly and disabled, there is a small bug (at least I think so): first, document. getElementById ("id "). readonly = "true"; set input to read-only. getElementById ("id "). readonly = "false" does not work when you remove the read-only attribute. In this case, you need to remove the quotation marks from the false field to the js statement for normal operation.

Appendix:

1. jquery sets and cancels read-only attributes through the id attribute

Set read-only: $ ("# id"). attr ("readOnly", "true ");

Cancel read-only: $ ("# id"). attr ("readOnly", false );

2. jquery batch set and cancel read-only attributes

/* All input boxes in p with id sa */

Set read-only: $ ("# sa input"). attr ("readOnly", "true ");

Cancel read-only: $ ("# sa input"). attr ("readOnly", false );

Related Articles:

Js sets the input text box to read-only

Set all form objects to read-only through js

Process the form to make input and other text boxes read-only and uneditable

The above is the details about using js to dynamically control the read-only attribute of the input box. For more information, see other related articles in the first PHP community!

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.