Javascript和defaultValue的隐藏输入元素 [英] Javascript and defaultValue of hidden input elements

查看:92
本文介绍了Javascript和defaultValue的隐藏输入元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设你有输入元素:

<input id="aaa" type="text" value="unchanged" />

然后启动js脚本:

var e = document.getElementById("aaa");
e.value = "changed";
alert(e.defaultValue + "/" + e.value);

结果将不变/更改。不幸的是,当您的输入元素为隐藏时:

Result will be "unchanged/changed". Unfortunately, when your input element is hidden:

<input id="aaa" type="hidden" value="unchanged" />

...同样的js脚本似乎没有工作了。结果是更改/更改。
这是一个正确的方法吗?如果是这样,为什么隐藏的表单元素不同?

...the same js script seem not to be working any more. Result is "changed/changed". Is this a proper way? If so, why only hidden form elements act different?

推荐答案

defaultValue属性只保存在你显然期望的方式对于文本,文件和密码字段。

The "defaultValue" property is only maintained in a way you apparently expect for "text", "file", and "password" fields.

这里是DOM规范的相关部分。

Here is the relevant portion of the DOM spec.

我怀疑这样做的原因是用户活动自身无法更改隐藏元素的值。如果要保留初始值,请运行加载或准备以将该值保存在某处。

I suspect the reason for this is that user activity on its own cannot change the value of hidden elements. If you want to preserve the initial values, run something at "load" or "ready" to stash the value somewhere.

这篇关于Javascript和defaultValue的隐藏输入元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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