Home > Web Front-end > JS Tutorial > body text

How to make an element read-only in jquery

WBOY
Release: 2021-12-13 13:49:31
Original
3080 people have browsed it

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")".

How to make an element read-only in jquery

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)
Copy after login

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>
Copy after login

Output results:

How to make an element read-only in jquery

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!