为什么html表单上的“重置"按钮不重置隐藏字段? [英] Why does the reset button on html forms not reset hidden fields?

查看:260
本文介绍了为什么html表单上的“重置"按钮不重置隐藏字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了一些令人惊讶的东西:

I discovered something surprising:

<html>
<head>
<script type="text/javascript">
function f()
{
document.getElementById("h").value++;
document.getElementById("x").value++;
}
</script>
</head>
<body>

<form>
<input type="hidden" name="hidden" id="h" value="5"/>
<input type="text" id="x" value="5"/>
<input name='clear' type='reset' id='clear' value='Clear'>
</form>

<button type="button" onclick="f()">Increment</button>
<button type="button" onclick="alert(document.getElementById('h').value)">Show hidden</button>

</body>
</html> 

在Firefox 4.0.1中尝试此操作,单击清除始终会将输入的文本重置为5,但是绝不会重置隐藏字段.

Trying this in Firefox 4.0.1, clicking clear always resets the text input to 5, but never resets the hidden field.

我(和其他人)根本没有期望这种行为:我们希望隐藏值也能重置!

I (and others) did not expect this behavior at all: we expected the hidden value to get reset too!

任何人都可以指向文档或说明来说明为何通过重置"按钮将隐藏的输入区别对待吗?

也欢迎解释为什么需要这种行为.

Explanations as to why such behavior is desirable are also welcome.

推荐答案

FWIW,我想我可以将答案和评论中的全部内容汇总在一起.

FWIW, I think I can put together the full story from the answers and comments.

使用原理:清除按钮用于清除用户输入,并且由于用户无法直接访问隐藏的输入,因此不允许用户重置隐藏的输入是没有意义的.值.

Usage rationale: The clear button is intended for clearing user input, and since hidden inputs are not directly accessible by the user, it doesn't make sense to allow the user to reset the hidden input's value.

文档和行为: AR指出的错误报告明确说明了正在发生的事情:隐藏字段的value的模式为默认,如规范.
特别是,这意味着更改值(如问题中的示例代码中的值)会更改默认值,并且重置"按钮会将输入字段重置为默认值,因此没有任何更改.
文本输入的行为有所不同(即使其值也已通过编程方式更改),因为其value的模式不是默认模式,而是

Documentation and behavior: The bug report that AR pointed out is explicit about what is happening: The hidden field's value's mode is default, as is intended in the specs.
Particularly, this means that changing the value (as in the sample code in the question) changes the default value, and the reset button resets input fields to the default value, hence there is no change.
The behavior for the text input is different (even though its value is also changed programmatically) because its value's mode is not default but value, which means that there is a distinction between the default value of the input and the current value.

这篇关于为什么html表单上的“重置"按钮不重置隐藏字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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