Home  >  Article  >  Web Front-end  >  How to change input to read-only status in jquery

How to change input to read-only status in jquery

青灯夜游
青灯夜游Original
2022-06-09 17:04:554193browse

Two implementation methods: 1. Use attr() to add the readonly attribute to the input element, and set the attribute value to "readonly", the syntax is "$("input").attr("readonly"," readonly");". 2. Use prop() to add the readonly attribute to the input element, and set the attribute value to "true", using the syntax "$("input").prop("readonly",true);".

How to change input to read-only status in jquery

The operating environment of this tutorial: windows7 system, jquery3.6.0 version, Dell G3 computer.

If you want to make the input read-only, just add the read-only attribute (readonly) to the input element.

Therefore, using jquery to change the input to a read-only state is converted to using jquery to add the readonly attribute to the input element.

There are two implementation methods:

  • Use attr()

  • Use prop()

1. Use attr() to set the read-only status of the input element



	
		
		
		
		
	


How to change input to read-only status in jquery

2. Use prop() Set the read-only status of the input element



	
		
		
		
		
	


How to change input to read-only status in jquery

Instructions:

In fact, the prop() method and the attr() method Similar, both are used to get or set the HTML attributes of elements, but there are essential differences between the two.

jQuery official recommendation: For attributes with two values ​​of true and false, such as checked, selected, disabled, etc., it is recommended to use the prop() method to operate, while for other attributes it is recommended to use the attr() method. to operate.

[Recommended learning: jQuery video tutorial, web front-end video]

The above is the detailed content of How to change input to read-only status in jquery. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn