Get the App
SLTechnology News&Howtos  ›  Development  › 

The method of setting text box read-only by jquery

Shulou Source: shulou.com Published: 2022-06-01 00:17:29 09月12日 Update

This article mainly introduces the relevant knowledge of "jquery set text box read-only method". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "jquery set text box read-only method" can help you solve the problem.

Methods: 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, set the readonly attribute value to readonly, and 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.

How to set the text box to read-only by jquery

1. Use the "$(input element)" statement to get the text box element object

$is another name for jQuery.

JQuery is a function provided by the jQuery library. (it seems to be more than just a function, because there is also a use like $.ajax (options), which is equivalent to jQuery.ajax (options)).

The function of this function is to find and select elements in the html document according to the parameters in (). One of the functions is to replace GetElementByID, but it can be not only an ID, but also a variety of selectors.

For example:

$(document) is to select the entire document object

2. Attr method

The attr () method sets or returns the properties and values of the selected element.

When the method is used to return a property value, it returns the value of the first matching element.

When this method is used to set property values, one or more attribute / value pairs are set for matching elements.

Grammar

Returns the value of the property:

$(selector) .attr (attribute)

Set properties and values:

$(selector) .attr (attribute,value)

The readonly property specifies that the input field is read-only.

Read-only fields cannot be modified. However, users can still use the tab key to switch to the field and select or copy its text.

Examples are as follows:

Runoob.com $(document) .ready (function () {$("button") .click (function () {$('input') .attr ("readonly", "readonly");}); set the text box to read-only

Output result:

This is the end of the introduction to "how to set the text box read-only by jquery". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Tags: Text methods elements attributes functions objects that is fields knowledge functions tutorials documents industries statements syntax choices different practical not just content Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Shulou Tech Info Shulou Information Xiaomi macOS