Detailed explanation of the subtle differences between Readonly and Disabled

高洛峰
Release: 2017-03-03 16:11:40
Original
1489 people have browsed it

Both Readonly and Disabled can prevent users from changing the content in the form field, but there are still certain differences between them. The following is a detailed introduction to you. Both Readonly and Disabled can prevent users from changing the contents of form fields. But there are slight differences between them, which are summarized as follows:

Readonly is only valid for input (text / password) and textarea, while disabled is valid for all form elements, but after the form element uses disabled, When we submit the form via POST or GET, the value of this element will not be passed out, but readonly will pass the value out (readonly accepts value changes and can return them, disable accepts changes but does not return data).

Generally common situations are:

A unique identification code is pre-filled in a form for the user, and the user is not allowed to change it, but the value needs to be passed when submitting. Its property should be set to readonly.

It is often encountered that when a user formally submits a form and needs to wait for information verification by the administrator, the user is not allowed to change the data in the form, but can only view it. Due to the large range of disabled elements, Therefore, disabled should be used at this time, but at the same time, it should be noted that the submit button should also be disabled. Otherwise, as long as the user presses this button, if there is no integrity check in the database operation page, the values ​​in the database will be cleared. . If readonly is used instead of disabled in this case, it is still possible if there are only input (text/password) and textarea elements in the form. If there are other elements, such as select, the user can rewrite the value and press Press the Enter key to submit (Enter is the default submit trigger key)

We often use javascript to disable the submit button after the user presses the submit button. This can prevent the user from using the submit button in an environment with poor network conditions. The user repeatedly clicks the submit button causing data to be redundantly stored in the database.

The two attributes disabled and readonly have something in common. For example, if both are set to true, the form attribute cannot be edited. It is often easy to mix these two attributes when writing js code. In fact, they There are certain differences between them:

If the disabled of an input item is set to true, the form input item cannot obtain focus, and all user operations (mouse clicks and keyboard input, etc.) will affect the input item. Invalid, the most important point is that when the form is submitted, this form input will not be submitted.
Readonly is only for input items such as text input boxes that can input text. If set to true, the user just cannot edit the corresponding text, but can still focus, and when submitting the form, the input item will be used as A submission of the form.

For more detailed explanations of the subtle differences between Readonly and Disabled, please pay attention to the PHP Chinese website for related articles!

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