disabled=“disabled”和disabled=“disabled”有什么区别?和 readonly=“只读” HTML 表单输入字段?

发布于 2024-12-09 01:57:48 字数 45 浏览 0 评论 0 原文

我读过一些相关内容,但我似乎找不到任何关于不同浏览器如何处理事物的可靠信息。

I have read a bit on this, but I can't seem to find anything solid about how different browsers treat things.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

四叶草在未来唯美盛开 2024-12-16 01:57:48

readonly 元素不可编辑,但会在相应的 form 提交时发送。 disabled 元素不可编辑,也不会在提交时发送。另一个区别是 readonly 元素可以获得焦点(并且在通过表单进行“Tab 键切换”时获得焦点),而 disabled 元素则不能。

阅读有关此内容的更多信息 这篇很棒的文章w3c 的定义。引用重要部分:

主要区别

禁用属性

  • 禁用的表单元素的值不会传递给处理器方法。 W3C 将此称为成功元素。(其工作原理类似于
    表单未选中的复选框。)
  • 某些浏览器可能会覆盖已禁用的表单元素或提供默认样式。 (灰色或浮雕文本)Internet Explorer
    5.5 在这方面尤其令人讨厌。
  • 禁用的表单元素不会获得焦点。
  • 在选项卡式导航中会跳过禁用的表单元素。

只读属性

  • 并非所有表单元素都具有只读属性。最值得注意的是,

A readonly element is just not editable, but gets sent when the according form submits. A disabled element isn't editable and isn't sent on submit. Another difference is that readonly elements can be focused (and getting focused when "tabbing" through a form) while disabled elements can't.

Read more about this in this great article or the definition by w3c. To quote the important part:

Key Differences

The Disabled attribute

  • Values for disabled form elements are not passed to the processor method. The W3C calls this a successful element.(This works similar to
    form check boxes that are not checked.)
  • Some browsers may override or provide default styling for disabled form elements. (Gray out or emboss text) Internet Explorer
    5.5 is particularly nasty about this.
  • Disabled form elements do not receive focus.
  • Disabled form elements are skipped in tabbing navigation.

The Read Only Attribute

  • Not all form elements have a readonly attribute. Most notable, the <SELECT> , <OPTION> , and <BUTTON> elements do not have readonly
    attributes (although they both have disabled attributes)
  • Browsers provide no default overridden visual feedback that the form element is read only. (This can be a problem… see below.)
  • Form elements with the readonly attribute set will get passed to the form processor.
  • Read only form elements can receive the focus
  • Read only form elements are included in tabbed navigation.
影子是时光的心 2024-12-16 01:57:48

当元素具有禁用属性时,不会触发任何事件。

以下任何一个都不会被触发。

$("[disabled]").click( function(){ console.log("clicked") });//No Impact
$("[disabled]").hover( function(){ console.log("hovered") });//No Impact
$("[disabled]").dblclick( function(){ console.log("double clicked") });//No Impact

而 readonly 则会被触发。

$("[readonly]").click( function(){ console.log("clicked") });//log - clicked
$("[readonly]").hover( function(){ console.log("hovered") });//log - hovered
$("[readonly]").dblclick( function(){ console.log("double clicked") });//log - double clicked

No events get triggered when the element is having disabled attribute.

None of the below will get triggered.

$("[disabled]").click( function(){ console.log("clicked") });//No Impact
$("[disabled]").hover( function(){ console.log("hovered") });//No Impact
$("[disabled]").dblclick( function(){ console.log("double clicked") });//No Impact

While readonly will be triggered.

$("[readonly]").click( function(){ console.log("clicked") });//log - clicked
$("[readonly]").hover( function(){ console.log("hovered") });//log - hovered
$("[readonly]").dblclick( function(){ console.log("double clicked") });//log - double clicked
陌伤ぢ 2024-12-16 01:57:48

禁用意味着提交表单时不会提交该表单元素中的任何数据。只读意味着元素内的任何数据都将被提交,但用户无法更改。

例如:

<input type="text" name="yourname" value="Bob" readonly="readonly" />

这将为元素“yourname”提交值“Bob”。

<input type="text" name="yourname" value="Bob" disabled="disabled" />

这不会为元素“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:

<input type="text" name="yourname" value="Bob" readonly="readonly" />

This will submit the value "Bob" for the element "yourname".

<input type="text" name="yourname" value="Bob" disabled="disabled" />

This will submit nothing for the element "yourname".

诠释孤独 2024-12-16 01:57:48

与其他答案相同(禁用不会发送到服务器,只读会发送到服务器),但某些浏览器会阻止突出显示禁用的表单,而只读仍可以突出显示(并复制)。

http://www.w3schools.com/tags/att_input_disabled.asp

http://www.w3schools.com/tags/att_input_readonly.asp

无法修改只读字段。但是,用户可以使用 Tab 键选中它、突出显示它并从中复制文本。

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

A read-only field cannot be modified. However, a user can tab to it, highlight it, and copy the text from it.

_畞蕅 2024-12-16 01:57:48

如果清除(重置)表单时需要保留禁用文本框的值,则必须使用 disabled = "disabled",因为只读文本框不会保留该值

例如:

HTML

文本框

<input type="text" id="disabledText" name="randombox" value="demo" disabled="disabled" />

重置按钮

<button type="reset" id="clearButton">Clear</button>

在上面的示例中,当按下“清除”按钮时,禁用的文本值将保留在表单中。 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 value

For Example:

HTML

Textbox

<input type="text" id="disabledText" name="randombox" value="demo" disabled="disabled" />

Reset button

<button type="reset" id="clearButton">Clear</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"

不交电费瞎发啥光 2024-12-16 01:57:48

禁用和只读之间的区别在于,只读控件仍然可以运行并且仍然可以获得焦点,而禁用的控件无法接收焦点并且不会随表单一起提交

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

红尘作伴 2024-12-16 01:57:48

可以设置 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

烟凡古楼 2024-12-16 01:57:48

基本上,只读属性意味着用户无法编辑该元素,但会与表单一起发送。
然而,禁用属性意味着用户无法编辑该元素,并且不会与表单一起发送。 (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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文