Html 禁用的表单字段未提交数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8925716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disabled form fields not submitting data
提问by bardiir
Is there any way (with a attribute flag or something like that) to enable form fields that are disabled to submit data?
有没有办法(使用属性标志或类似的东西)来启用禁用提交数据的表单字段?
Or, if that's not possible, is there any way to block fields from editing with css or any other attribute than disabled without hiding them?
或者,如果这是不可能的,有没有办法阻止使用 css 或任何其他属性编辑字段而不隐藏它们?
My special case at the moment is an identifier for a data-set that should be shown in the form (uneditable) - if there is no better solution I think i'll use a hidden field in addition to the disabled one to hold the actual value with the disabled one showing it.
我目前的特殊情况是数据集的标识符,该标识符应显示在表单中(不可编辑)-如果没有更好的解决方案,我想除了禁用的字段之外,我还会使用隐藏字段来保存实际值与禁用的显示它。
回答by Mario Werner
As it was already mentioned: READONLY
does not work for <input type='checkbox'>
and <select>...</select>
.
正如已经提到的:READONLY
不适用于<input type='checkbox'>
and <select>...</select>
。
If you have a Form
with disabled checkboxes / selects AND need them to be submitted, you can use jQuery:
如果您有Form
禁用复选框/选择并且需要提交它们,您可以使用 jQuery:
$('form').submit(function(e) {
$(':disabled').each(function(e) {
$(this).removeAttr('disabled');
})
});
This code removes the disabled
attribute from all elements on submit.
此代码disabled
在提交时从所有元素中删除该属性。
回答by Tom
Use the CSS pointer-events:none on fields you want to "disable" (possibly together with a greyed background) which allows the POST action, like:
在要“禁用”(可能与灰色背景一起)的字段上使用 CSS pointer-events:none 允许 POST 操作,例如:
<input type="text" class="disable">
.disable{
pointer-events:none;
background:grey;
}
Ref: https://developer.mozilla.org/en-US/docs/Web/CSS/pointer-events
参考:https: //developer.mozilla.org/en-US/docs/Web/CSS/pointer-events
回答by Kripa Yadav
We can also use the readonly only with below attributes -
我们还可以将 readonly 与以下属性一起使用 -
readonly onclick='return false;'
只读 onclick='return false;'
This is because if we will only use the readonly then radio buttons will be editable. To avoid this situation we can use readonly with above combination. It will restrict the editing and element's values will also passed during form submission.
这是因为如果我们只使用 readonly 那么单选按钮将是可编辑的。为了避免这种情况,我们可以将 readonly 与上述组合一起使用。它将限制编辑并且元素的值也将在表单提交期间传递。
回答by Paresh
add CSS or class to the input element which works in select and text tags like
将 CSS 或类添加到适用于选择和文本标签的输入元素,如
style="pointer-events: none;background-color:#E9ECEF"
style="pointer-events: none;background-color:#E9ECEF"