开发者

Why does grails add a hidden checkbox to a form - and what does it do with it?

开发者 https://www.devze.com 2022-12-16 20:09 出处:网络
I have a checkbox on my GSP page as follows (which was stolen directly from the scaffolded \"create\" code for my domain object)...

I have a checkbox on my GSP page as follows (which was stolen directly from the scaffolded "create" code for my domain object)...

<tr class="prop">
<td valign="top" class="name">
  <label for="isSelling"><g:message code="person.isSelling.label" default="Is Selling" /></label>
</td>
<td valign="top" class="value ${hasErrors(bean: personInstance, field: 'isSelling', 'errors')}">
    <g:checkBox name="isSelling" value="${personInstance?.isSelling}" />
</td>
</tr>

This works just fine, except when I look at the elements in the resulting form I have a hidden checkbox alongside the real one...

<tr class="prop">
<td valign="top" class="name">
  <label for="isSelling">Is Selling</label>
</td>
<td valign="top" class="value ">
    <input type="hidden" name="_isSelling" />
    <input type="checkbox" name="isSelling" id="isSelling"  />
</td>
</tr>

My questions are:

  1. why is it there?
  2. what does Grails do with it?
  3. if I am looking at the form values in Javascript, which input value sh开发者_运维问答ould I take?

Just inspecting what happens when the checkbox gets set on and off in my page, it appears that the hidden one is ignored, so I am imagining there is some cunning processing going on when the submit action occurs which looks at the _isSelling and isSelling for some magical purpose. Anyone have any insight into what Grails is doing?

Thanks


That's a spring thing. It adds that checkbox so that unchecked boxes are accountable. Some browsers won't push any information about an unchecked box so the hidden box is added to prevent binding errors.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号