Home >Web Front-end >HTML Tutorial >How to disable form controls in HTML: readonly and disabled
Sometimes we want the controls on the form to be unmodifiable. For example, in a web page for changing passwords, the text box that displays the user name should be unmodifiable. Here are two ways to disable controls in the form. method
In the process of making web pages, we often use forms. But sometimes we want the controls on the form to be unmodifiable. For example, in a web page for changing passwords, the text box that displays the user name should be unmodifiable.
There are two disabling methods in HTML, they are:
1. Add the readonly='readonly' attribute to the control label
2. Add disabled to the control label ='disabled' attribute
Example:
The code is as follows:
<input type="text" value="只读的" readonly="readonly" /> <input type="text" value="不可用的" disabled="disabled" />
The two controls in the example cannot be modified. But they also have some differences!
From the literal meaning, we can know that the control using the readonly attribute is "read-only", while the control using disabled is "disabled". So what's the difference between them?
Read-only controls (controls that use the readonly attribute) cannot be modified by users with the mouse and keyboard, but programmers can use JavaScript to modify the name and value of the control when submitting the form. It will be submitted to the server, which means it is visible to the server.
The disabled control (control using the disabled attribute) not only cannot be modified by the user with the mouse and keyboard, but also is invisible to the server, that is, its name and value will not be submitted to the server when the form is submitted. , of course, programmers can also use javascript to modify its value.
This is their difference. After knowing the difference, we should know when to use which method. We must not be blind!
For more articles related to readonly and disabled methods of disabling form controls in HTML, please pay attention to the PHP Chinese website!