Jquery method to change an element to read-only: 1. Use the "$(specified element)" statement to obtain the specified element object; 2. Use the attr() method and readonly attribute to change the element to read-only status. The syntax is "element object.attr("readonly","readonly")".
The operating environment of this tutorial: windows10 system, jquery3.2.1 version, Dell G3 computer.
How to change an element to read-only in jquery
In css, you can use the attr() method to change an element to a read-only state. The attr() method sets or returns the attribute value of the selected element.
The syntax is:
$(selector).attr(attribute,value)
Set the readonly attribute of the element to readonly. The readOnly attribute sets or returns whether the text field is read-only.
Let’s take an example to see how to set the text box element to read-only status. Examples are as follows:
<html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> function demo(){ $("input[name=demo]").attr("readonly","readonly");} </script> </head> <body> <input type="text" name="demo"/><input type="button" onclick="demo();" value="修改"/> </body> </html>
Output results:
Related video tutorial recommendations: jQuery video tutorial
The above is the detailed content of How to make an element read-only in jquery. For more information, please follow other related articles on the PHP Chinese website!