如何从禁用的< input>获取表单值?元件 [英] How can I get the form value from a disabled <input> element

查看:73
本文介绍了如何从禁用的< input>获取表单值?元件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

表单的HTML标准似乎是这样的,禁用的输入元素不会影响表单名称/值集合。



有没有办法绕过这个?我需要能够打开和关闭禁用,但仍然返回提交表单时的值。



我意识到我可以使用JavaScript来复制值提交表单之前隐藏的输入,但我不想。有没有更清晰的方式?



我使用的是ASP.NET,并不重要。

解决方案您应该使用 readOnly 标志而不是 disabled

 < input type =text只读字段不能由用户编辑,但仍然会以表单提交。 value =blahreadonly =true/> 


The HTML standard for forms appears to be such that disabled input elements do not contribute to the form name/value collection.

Is there ANY way to get around this? I need to be able to toggle disable on and off but still return whatever the value is when the form is submitted.

I realize that I can use JavaScript to copy the value to a hidden input before the form is submitted, but I would prefer not to. Is there a cleaner way?

I'm using ASP.NET, not that that matters.

解决方案

You should use the readOnly flag rather than disabled. Read-only fields cannot be edited by the user, but are still submitted with the form.

<input type="text" value="blah" readonly="true"/>

这篇关于如何从禁用的&lt; input&gt;获取表单值?元件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆