我正在使用Spring MVC开发一个web应用程序。在表单上,我有
<form:checkbox path="agreeTerms" id="agreeTerms"/>
呈现页面时,将生成以下HTML
<input id="agreeTerms" type="checkbox" value="true" name="agreeTerms">
<input type="hidden" value="on" name="_agreeTerms">
有人知道隐藏标签的用途吗?如果删除隐藏的input标记,会发生什么?
谢谢!
发布于 2013-06-28 23:45:02
隐藏的input标记用于指示最初是表单一部分的字段。提交表单时,仅当复选框输入字段具有值(即'checked')时才会发送。如果未选中,则不会发送任何内容。下划线前缀隐藏字段用于指示它是表单的一部分,但应默认为“未选中/假”。
您可以通过创建一个带有复选框字段的HTML表单并在不选中该字段的情况下提交它来测试这一点。
此外,要了解它是如何完成的,请查看WebDataBinder的源代码
/**
* Check the given property values for field markers,
* i.e. for fields that start with the field marker prefix.
* <p>The existence of a field marker indicates that the specified
* field existed in the form. If the property values do not contain
* a corresponding field value, the field will be considered as empty
* and will be reset appropriately.
* @param mpvs the property values to be bound (can be modified)
* @see #getFieldMarkerPrefix
* @see #getEmptyValue(String, Class)
*/
https://stackoverflow.com/questions/17368168
复制相似问题