disabled=“disabled”和disabled=“disabled”有什么区别?和 readonly=“只读” HTML 表单输入字段?
我读过一些相关内容,但我似乎找不到任何关于不同浏览器如何处理事物的可靠信息。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我读过一些相关内容,但我似乎找不到任何关于不同浏览器如何处理事物的可靠信息。
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(8)
readonly
元素不可编辑,但会在相应的form
提交时发送。disabled
元素不可编辑,也不会在提交时发送。另一个区别是readonly
元素可以获得焦点(并且在通过表单进行“Tab 键切换”时获得焦点),而disabled
元素则不能。阅读有关此内容的更多信息 这篇很棒的文章 或 w3c 的定义。引用重要部分:
A
readonly
element is just not editable, but gets sent when the accordingform
submits. Adisabled
element isn't editable and isn't sent on submit. Another difference is thatreadonly
elements can be focused (and getting focused when "tabbing" through a form) whiledisabled
elements can't.Read more about this in this great article or the definition by w3c. To quote the important part:
当元素具有禁用属性时,不会触发任何事件。
以下任何一个都不会被触发。
而 readonly 则会被触发。
No events get triggered when the element is having disabled attribute.
None of the below will get triggered.
While readonly will be triggered.
禁用意味着提交表单时不会提交该表单元素中的任何数据。只读意味着元素内的任何数据都将被提交,但用户无法更改。
例如:
这将为元素“yourname”提交值“Bob”。
这不会为元素“yourname”提交任何内容。
Disabled means that no data from that form element will be submitted when the form is submitted. Read-only means any data from within the element will be submitted, but it cannot be changed by the user.
For example:
This will submit the value "Bob" for the element "yourname".
This will submit nothing for the element "yourname".
与其他答案相同(禁用不会发送到服务器,只读会发送到服务器),但某些浏览器会阻止突出显示禁用的表单,而只读仍可以突出显示(并复制)。
http://www.w3schools.com/tags/att_input_disabled.asp
http://www.w3schools.com/tags/att_input_readonly.asp
Same as the other answers (disabled isn't sent to the server, readonly is) but some browsers prevent highlighting of a disabled form, while read-only can still be highlighted (and copied).
http://www.w3schools.com/tags/att_input_disabled.asp
http://www.w3schools.com/tags/att_input_readonly.asp
如果清除(重置)表单时需要保留禁用文本框的值,则必须使用
disabled = "disabled"
,因为只读文本框不会保留该值例如:
HTML
文本框
重置按钮
在上面的示例中,当按下“清除”按钮时,禁用的文本值将保留在表单中。
input type = "text" readonly="readonly" 的情况下不会保留值
If the value of a disabled textbox needs to be retained when a form is cleared (reset),
disabled = "disabled"
has to be used, as read-only textbox will not retain the valueFor Example:
HTML
Textbox
Reset button
In the above example, when Clear button is pressed, disabled text value will be retained in the form. Value will not be retained in the case of
input type = "text" readonly="readonly"
禁用和只读之间的区别在于,只读控件仍然可以运行并且仍然可以获得焦点,而禁用的控件无法接收焦点并且不会随表单一起提交
The difference between disabled and readonly is that read-only controls can still function and are still focusable, anddisabled controls can not receive focus and are not submitted with the form
可以设置 readonly 属性以防止用户更改值,直到满足其他一些条件,而可以设置 invalid 属性以防止用户使用该元素
The readonly attribute can be set to keep a user from changing the value until some other conditions have been met while the disabled attribute can be set to keep a user from using the element
基本上,只读属性意味着用户无法编辑该元素,但会与表单一起发送。
然而,禁用属性意味着用户无法编辑该元素,并且不会与表单一起发送。 (PS 禁用元素的不透明度也较低)
Basically, a read-only attribute means the element can't be edited by the user, but is sent with the form.
A disabled attribute however means that the element can't be edited by the user, and won't be sent with the form. (P.S. Disabled elements also have lower opacity)