为什么check_box表单帮助程序会生成两个复选框,其中一个隐藏? [英] Why does the check_box form helper generate two checkboxes, one hidden?

查看:102
本文介绍了为什么check_box表单帮助程序会生成两个复选框,其中一个隐藏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码: = form_fo:store_products做| f | = f.check_box:track_inventory

this code: =form_fo :store_products do |f| = f.check_box :track_inventory

创建此html:

<input name="product_group[products_attributes][0][store_products_attributes}[1342647745501][track_inventory]" type="hidden" value="0">

<input id="product_group_products_attributes_0_store_products_attributes_1342647745501_track_inventory" name="product_group[products_attributes][0][store_products_attributes][1342647745501][track_inventory]" type="checkbox" value="1">

第一个隐藏元素的原因是什么?

What is the reason for the first hidden element?

推荐答案

HTML规范规定,Web浏览器不应发送未选中的复选框.这意味着,如果未选中,则rails不会收到有关表单上的复选框是否未选中的记录.例如,如果用户正在编辑先前已选中该复选框的记录,而他们决定取消选中该记录,这将很重要-Rails将不知道更新此属性.

The HTML specification says that unchecked checkboxes should not be sent by webbrowsers. This means that, if unchecked, rails receives no record of whether the checkbox on the form was unchecked. This would be important, for example, if the user was editing a record where the checkbox was previously checked and they had decided to uncheck it - rails would not know to update this attribute.

隐藏字段与复选​​框具有相同的名称,因此,如果未发送复选框,则将发送hidden_​​field(值为'0',表示未选中).这样,rails将始终收到有关该复选框处于选中状态还是未选中状态的信号.

The hidden field has the same name as the checkbox, so if the checkbox is not sent, the hidden_field is sent instead (with the value of '0', meaning unchecked). This way, rails will always receive a signal as to whether the checkbox was checked or unchecked.

有关此陷阱的更多信息,请访问 APIDock

More information on this gotcha at APIDock

这篇关于为什么check_box表单帮助程序会生成两个复选框,其中一个隐藏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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