Both readonly and disabled can prevent users from modifying the content in form fields. 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. 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, and readonly will pass this value out (readonly can accept the value change and return it, disable accepts changes but does not return data ).
Generally, the following situations are commonly used:
- A unique identifier is pre-filled in for the user in a form.CodeThe user is not allowed to change it, but 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 (Press enter as the default submit trigger button)
- 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.
Address: http://www.cnblogs.com/zcy_soft/archive/2011/09/19/2181211.html