这在浏览器中意味着什么:“将不会提交禁用输入的值”?

发布于 2024-08-03 23:16:31 字数 150 浏览 6 评论 0原文

这是我在 Firefox 中通过 Firebug 发现的。

Values of disabled inputs will not be submitted

在其他浏览器中也是这样吗?

如果是这样,原因是什么?

This is what I found by Firebug in Firefox.

Values of disabled inputs will not be submitted

Is it the same in other browsers?

If so, what's the reason for this?

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

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

发布评论

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

评论(9

開玄 2024-08-10 23:16:31

disabled 输入将不会提交数据。

使用 readonly 属性:

<input type="text" name="inputName" readonly />

来源在这里

disabled input will not submit data.

Use the readonly attribute:

<input type="text" name="inputName" readonly />

Source here

山田美奈子 2024-08-10 23:16:31

是的,所有浏览器都不应提交禁用的输入。

更多信息(第 17.12.1 节)

属性定义

disabled [CI] 当为表单控件设置时,此布尔属性
禁用用户输入的控件。设置后,禁用属性
对元素有以下影响:

  • 禁用的控件不会获得焦点。
  • 在选项卡导航中会跳过禁用的控件。
  • 禁用的控件无法成功。

以下元素支持禁用属性:BUTTON、INPUT、
OPTGROUP、OPTION、SELECT 和 TEXTAREA。

该属性是继承的,但本地声明会覆盖
继承价值。

如何呈现禁用元素取决于用户代理。为了
例如,某些用户代理“灰显”禁用菜单项、按钮
标签等

在此示例中,INPUT 元素被禁用。因此,它不能
接收用户输入也不会随表单提交其值

<输入已禁用 name="fred" value="stone">

注意。动态修改disabled值的唯一方法
属性是通过脚本实现的。

Yes, all browsers should not submit the disabled inputs.

More information (section 17.12.1)

Attribute definitions

disabled [CI] When set for a form control, this Boolean attribute
disables the control for user input. When set, the disabled attribute
has the following effects on an element:

  • Disabled controls do not receive focus.
  • Disabled controls are skipped in tabbing navigation.
  • Disabled controls cannot be successful.

The following elements support the disabled attribute: BUTTON, INPUT,
OPTGROUP, OPTION, SELECT, and TEXTAREA.

This attribute is inherited but local declarations override the
inherited value.

How disabled elements are rendered depends on the user agent. For
example, some user agents "gray out" disabled menu items, button
labels, etc.

In this example, the INPUT element is disabled. Therefore, it cannot
receive user input nor will its value be submitted with the form.

<INPUT disabled name="fred" value="stone">

Note. The only way to modify dynamically the value of the disabled
attribute is through a script.

执手闯天涯 2024-08-10 23:16:31

您可以使用三种方式来模拟禁用:

  1. HTML:readonly属性(以便输入中存在的值可以在表单提交时使用。用户也无法更改输入值)< /p>

  2. CSS: 'pointer -events':'none' (阻止用户点击输入)

  3. HTML: tabindex="-1" (阻止用户从键盘导航到输入)

You can use three things to mimic disabled:

  1. HTML: readonly attribute (so that the value present in input can be used on form submission. Also the user can't change the input value)

  2. CSS: 'pointer-events':'none' (blocking the user from clicking the input)

  3. HTML: tabindex="-1" (blocking the user to navigate to the input from the keyboard)

跨年 2024-08-10 23:16:31

它们不会被提交,因为这就是它在W3C 规范

17.13.2 成功控制

成功的控件对于提交来说是“有效”的。 [截图]

  • 禁用的控件无法成功。

换句话说,规范规定禁用的控件将被视为提交无效。

They don't get submitted, because that's what it says in the W3C specification.

17.13.2 Successful controls

A successful control is "valid" for submission. [snip]

  • Controls that are disabled cannot be successful.

In other words, the specification says that controls that are disabled are considered invalid for submission.

听不够的曲调 2024-08-10 23:16:31
<input type="text" disabled /> 

而不是这个禁用使用只读

<input type="text" readonly />
<input type="text" disabled /> 

instead of this disabled use readonly

<input type="text" readonly />
吖咩 2024-08-10 23:16:31

有两个属性,即 readonlydisabled,可以使输入成为半只读。但它们之间有一个微小的区别。

<input type="text" readonly />
<input type="text" disabled />
  • readonly 属性使您的输入文本被禁用,用户无法再更改它。
  • disabled 属性不仅会使您的输入文本禁用(不可更改),而且无法提交

jQuery 方法(1):

$("#inputID").prop("readonly", true);
$("#inputID").prop("disabled", true);

jQuery 方法(2):

$("#inputID").attr("readonly","readonly");
$("#inputID").attr("disabled", "disabled");

JavaScript 方法:

document.getElementById("inputID").readOnly = true;
document.getElementById("inputID").disabled = true;

PS disabledreadonly 是标准的 html 属性。 propjQuery 1.6 引入。

There are two attributes, namely readonly and disabled, that can make a semi-read-only input. But there is a tiny difference between them.

<input type="text" readonly />
<input type="text" disabled />
  • The readonly attribute makes your input text disabled, and users are not able to change it anymore.
  • Not only will the disabled attribute make your input-text disabled(unchangeable) but also cannot it be submitted.

jQuery approach (1):

$("#inputID").prop("readonly", true);
$("#inputID").prop("disabled", true);

jQuery approach (2):

$("#inputID").attr("readonly","readonly");
$("#inputID").attr("disabled", "disabled");

JavaScript approach:

document.getElementById("inputID").readOnly = true;
document.getElementById("inputID").disabled = true;

PS disabled and readonly are standard html attributes. prop introduced with jQuery 1.6.

无可置疑 2024-08-10 23:16:31

禁用控件无法成功,成功的控件对于提交来说是“有效”的。
这就是禁用的控件不随表单提交的原因。

Disabled controls cannot be successful, and a successful control is "valid" for submission.
This is the reason why disabled controls don't submit with the form.

<逆流佳人身旁 2024-08-10 23:16:31

即使在只读属性上,选择控件仍然可单击

如果您仍想禁用该控件但希望发布其值,则 。
您可以考虑创建一个隐藏字段。与您的控件具有相同的值。

然后创建一个jquery,选择更改

$('#your_select_id').change(function () {
    $('#your_hidden_selectid').val($('#your_select_id').val());
});

select controls are still clickable even on readonly attrib

if you want to still disable the control but you want its value posted.
You might consider creating a hidden field. with the same value as your control.

then create a jquery, on select change

$('#your_select_id').change(function () {
    $('#your_hidden_selectid').val($('#your_select_id').val());
});
假情假意假温柔 2024-08-10 23:16:31

这是解决方案,仍然使用禁用的属性。
首先禁用加载时的输入。

$(document).ready(function(){
  $("formselector:input").prop("disabled",true);
  $( "formselector" ).submit(function( event ) {
      $(":disabled").prop("disabled",false);
      });
});

提交时启用所有这些。这将确保所有内容都已发布

Here's the Solution and still using disabled property.
First disable your inputs on load.

$(document).ready(function(){
  $("formselector:input").prop("disabled",true);
  $( "formselector" ).submit(function( event ) {
      $(":disabled").prop("disabled",false);
      });
});

on submit enable all of them. this will assure everything is posted

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