Method: 1. Use the "$(input element)" statement to obtain the text box element object; 2. Use the attr() method to set the text box read-only, and set the readonly attribute value to readonly. The syntax is: "Text box element object.attr("readonly","readonly")".
The operating environment of this tutorial: windows10 system, jquery3.2.1 version, Dell G3 computer.
1. Use the "$(input element)" statement to obtain the text box element object
$ is jQuery Another name for
And jQuery is a function provided by the jQuery library. (It seems that it is not just a function, because there is also the use of $.ajax(options), which is equivalent to jQuery.ajax(options))
The function of this function is to search and select elements in the html document based on the parameters in (). One of the functions of the function is to replace GetElementByID, but () can not only be ID, but also various selectors
For example:
$(document) is to select the entire document object
2, attr method
$(selector).attr(attribute)
$(selector).attr(attribute,value)
<html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("button").click(function(){ $('input').attr("readonly","readonly"); }); }); </script> </head> <body> <input type="text" name="FirstName" value="Mickey"> <button>将文本框设置为只读</button> </body> </html>
The above is the detailed content of How to set text box read-only in jquery. For more information, please follow other related articles on the PHP Chinese website!