Javascript 提交禁用输入字段的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4045648/
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
Submitting the value of a disabled input field
提问by Harsha M V
I want to disable an input field, but when I submit the form it should still pass the value.
我想禁用输入字段,但是当我提交表单时,它仍应传递该值。
Use case: I am trying to get latitude and longitude from Google Maps and wanna display it, but I don't want the user to edit it.
用例:我试图从谷歌地图获取纬度和经度并想显示它,但我不希望用户编辑它。
Is this possible?
这可能吗?
回答by Sarfraz
I wanna Disable an Input Field on a form and when i submit the form the values from the disabled form is not submitted.
Use Case: i am trying to get Lat Lng from Google Map and wanna Display it.. but dont want the user to edit it.
我想禁用表单上的输入字段,当我提交表单时,未提交禁用表单中的值。
用例:我试图从 Google 地图获取 Lat Lng 并想显示它...... 但不希望用户编辑它。
You can use the readonly
property in your input field
您可以readonly
在输入字段中使用该属性
<input type="text" readonly="readonly" />
回答by Abu Sulaiman
I know this is old but I just ran into this problem and none of the answers are suitable. nickf's solution works but it requires javascript. The best way is to disable the field and still pass the value is to use a hidden input field to pass the value to the form. For example,
我知道这很旧,但我刚刚遇到了这个问题,但没有一个答案是合适的。nickf 的解决方案有效,但它需要 javascript。最好的方法是禁用该字段并仍然传递值是使用隐藏的输入字段将值传递给表单。例如,
<input type="text" value="22.2222" disabled="disabled" />
<input type="hidden" name="lat" value="22.2222" />
This way the value is passed but the user sees the greyed out field. The readonly attribute does not gray it out.
通过这种方式传递值,但用户会看到灰色的字段。readonly 属性不会使其变灰。
回答by pjnovas
you can also use the Readonly attribute: the input is not gonna be grayed but it won't be editable
您还可以使用 Readonly 属性:输入不会变灰但不可编辑
<input type="text" name="lat" value="22.2222" readonly="readonly" />
回答by nickf
Input elements have a property called disabled
. When the form submits, just run some code like this:
输入元素有一个名为 的属性disabled
。当表单提交时,只需运行一些这样的代码:
var myInput = document.getElementById('myInput');
myInput.disabled = true;