简体   繁体   English

处理已选中和未选中的复选框

[英]Handle checked and unchecked checkbox

I have a very confusing situation here. 我这里的情况非常混乱。

<tr>
    <td>
        <input type="checkbox" id="box1" name="box1" <% if (renderRequest.getPreferences().getValue("box1", null).equals("on")) { %> checked="checked"  <% } %> >
    </td>
    <td><label>Box</label></td>
</tr>

In the portlet.xml I have set the box1 value as "on" by default. 在portlet.xml中,我默认将box1值设置为“ on”。 So when I load the edit.jsp page I see the value as checked. 因此,当我加载edit.jsp页面时,我看到了选中的值。 Now, I want to handle the situation of unchecked checkbox which I am confused about. 现在,我想处理令我感到困惑的未选中复选框的情况。 When I uncheck the box the value submitted is null and I am confused to handle the situation. 当我取消选中该复选框时,提交的值为null,我很困惑以应对这种情况。 How to submit a default value if the checkbox is unchecked. 如果未选中此复选框,则如何提交默认值。

Though this question was asked 3 years ago, I'm adding an answer for future viewers. 尽管这个问题是3年前提出的,但我为将来的观众添加了一个答案。

If a checkbox is unchecked, it doesn't get sent. 如果未选中此复选框,则不会发送该复选框。 So devang 's answer of setting it's value to "on/off" if it isn't checked isn't going to help - it will always return NULL. 因此devang将其值设置为“ on / off”(如果未选中)的答案无济于事-它始终返回NULL。

The easiest solution I have found is to add a hidden field with the same name that will be used if the check-box is unchecked. 我发现最简单的解决方案是添加一个隐藏字段,该字段的名称与未选中复选框时将使用的名称相同。

For eg, in your code, a solution could be 例如,在您的代码中,一个解决方案可能是

<tr>
    <td>
        <input type="checkbox" id="box1" name="box1" value="1" <% if (renderRequest.getPreferences().getValue("box1", null).equals("on")) { %> checked="checked"  <% } %> >
    </td>
    <td>
        <input type="checkbox" id="box1_hidden" name="box1" value="0" >
    </td>
</tr>

The above piece of code depends on the server handling of parameters as browser will send both the parameters. 上面的代码取决于服务器对参数的处理,因为浏览器将发送这两个参数。 If the checkbox is unchecked, the hidden field's value gets used as-is (since browser does not sends the null parameter). 如果未选中此复选框,则隐藏字段的值将按原样使用(因为浏览器不会发送null参数)。 However, things become cumbersome if the checkbox is checked, since now the browser sees two values corresponding to the same name field. 但是,如果选中此复选框,则事情变得很麻烦,因为现在浏览器会看到两个对应于相同名称字段的值。

Java for example, offers only the first value of a field with a particular name. 例如,Java仅提供具有特定名称的字段的第一个值。 PHP, on the other hand, takes the last value as the one to use. 另一方面,PHP将最后一个值用作要使用的值。

Thus, you need to perform one extra step and invoke JavaScript. 因此,您需要执行一个额外的步骤并调用JavaScript。 The idea is to disable the hidden input based on the checked condition. 这个想法是基于检查的条件禁用隐藏的输入。

if(document.getElementById("box1").checked) {
     document.getElementById('box1_hidden').disabled = true;
}

This constitutes the complete solution irrespective of the server handling. 无论服务器如何处理,这都构成了完整的解决方案。

As far as I remember, adding value attribute and set it to the value you want should work. 据我记得,添加value属性并将其设置为所需的值应该可以工作。 Below, value="on" is added. 在下面,添加value="on"

<input type="checkbox" id="box1" name="box1" value="on" 
  <% if (renderRequest.getPreferences().getValue("box1", null).equals("on")) { %>    
    checked="checked"  
  <% } %> >

使用剃刀语法:

<input type="checkbox" name="xxx" value="xxx" @if(condition == true) {<text>checked="checked"</Text>}>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM