About Readonly and Disabled in the form

Source: Internet
Author: User

Readonly and Disabled are two attributes used in a form. They both enable users not to change the content in the form field. However, there are minor 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, including select, radio, checkbox, And button.

However, after a form element is disabled, when we submit the form in POST or GET mode, the value of this element will not be passed out, readonly will pass this value out (this occurs when we set the textarea element in a form to disabled or readonly, but the submit button is usable ).

Generally, the following situations are commonly used:

  • In a form, a unique recognition code is pre-filled for the user, which cannot be modified by the user. However, this value must be passed during submission. In this case, set its attribute to readonly.
  • Often, when a user submits a form, he or she needs to wait for the Administrator to verify the information. This does not allow the user to change the data in the form. Instead, he or she can only view the data. Because disabled has a large scope of elements, therefore, you should use disabled at this time, but note that the submit button should also be disabled. Otherwise, if the user presses this button, if the database operation page does not perform Integrity Detection, the value in the database is cleared.
  • In this case, if we use readonly to replace disabled, we can still use only the input (text/password) and textarea elements in the form. If there are other sending elements, such as select, you can press the Enter key to submit the changes (the default submit trigger button is press Enter ).

  • After the user presses the submit button, the submit button is disabled by using javascript, which can prevent poor network conditions, the user repeatedly clicks the submit button, causing redundant data to be stored in the database.

The disabled and readonly attributes share some similarities. For example, if both attributes are set to true, the form attributes cannot be edited. These attributes are often used together when writing JavaScript code, in fact, there are some differences between them.

If the disabled of an input item is set to true, the input item in the form cannot obtain the focus. All user operations (such as mouse clicks and keyboard input) are invalid for this input item, the most important thing is that when a form is submitted, this form input item will not be submitted.

Readonly only applies to text input items such as text input boxes. If it is set to true, users only cannot edit the corresponding text, but can still focus, and when submitting a form, this input item will be submitted as a form.

Tip: diabled can be replaced by readonly. background-color: # cccccc; add a gray background color.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">   

When the input field is diabled, the value cannot be obtained from time to time. Therefore, we can replace it with readonly to solve this problem.

 

This value is not obtained after being submitted in the form.

Change disabled = "disabled" to readonly = "readonly" and follow W3C specifications: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.12

If the input parameter is set to disabled, the following restrictions apply:

  • Cannot receive focus
  • The tab key is skipped.
  • It may not be successful's

The following restrictions apply to input with readonly:

  • Can receive the focus but cannot be modified
  • You can use the tab key for navigation.
  • It may be successful's

Only the form element of successful is valid data, that is, it can be submitted. The disabled and readonly text input boxes can only be modified using scripts.

The effects and usage of these two attributes are different:

  1. Readonly is used to lock the control and cannot be modified on the interface (but it can be modified through javascript ).
  2. In the same way as readonly, the user can lock this control and cannot change its value. However, disabled is more thorough, so that you cannot use it at all, this includes changing the background color (do not believe it, you modify an input text box that is disabled, and you find that you are in vain). If it is a checkbox, you cannot select it.
  3. All controls have the disabled attribute, but not necessarily have the readonly attribute, such as the select drop-down box. Clicking the "readonly" button can trigger the event, but the "disabled" button cannot be used, regardless of the event.

After setting the disabled attribute for div, the entire div is grayed out, but the content can still be entered in the text box. Note: The select drop-down box does not have the readonly attribute.

When we write a program, for example, a page for purchasing a product, we can enter the quantity of the product, however, the price is set to read-only, and the total price below is the price of the Number * read-only, so we are likely to do this by reading the textbox value of the number of items in the background, then read the textbox value of the price, and the product of the two will be used as the total amount uodate to the database. If the client user is a small programmer who will point the program like ours, in this case, you may need to pull it. You can view the source file, the id or name of the text box at readonly price, and enter a script in the browser to change the price value, then click Submit, so this small programmer can give himself a discount at will.

After setting the form submission method to get submission, we can see the submitted content in the url and find that the control set to disabled is not submitted to the server, readonly data is submitted to the server.

The following is a reference code for controlling disabled and readonly with js:

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN "" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <Html xmlns =" http://www.w3.org/1999/xhtml "> <Head> <meta http-equiv =" Content-Type "content =" text/html; charset = UTF-8 "/> <title> untitled document </title> 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.