Home > Article > Web Front-end > Use js to dynamically control the read-only attribute of the input box
The read-only attribute of the input box: readonly
When added directly to the page as read-only, you can directly add readonly="readonly" to the input, but if you want to click If the button is changed, it needs to be implemented through js (or jquery).
The last time I used this, I finally discovered why I was always so depressed when writing this js control. It turns out that js has a small bug when setting attributes such as readonly and disabled (at least that's how I do it) Think): First, document.getElementById("id").readonly = "true"; sets the input to read-only, but when the read-only attribute is removed through document.getElementById("id").readonly="false" , has no effect. At this time, you need to remove the quotes outside false in order for the js statement to work properly.
Attachment:
1. jquery Set and cancel read-only attributes through the id attribute
Set read-only: $("#id").attr("readOnly","true");
Cancel read-only: $("#id"). attr("readOnly",false);
2. jquery Batch setting and canceling 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 be read-only
Set all form objects through js Read-only
Methods for processing forms so that input and other text boxes are read-only and non-editable
The above is the detailed content of Use js to dynamically control the read-only attribute of the input box. For more information, please follow other related articles on the PHP Chinese website!