【问题标题】:Grails checkbox always falseGrails 复选框始终为 false
【发布时间】:2018-06-21 15:40:56
【问题描述】:

第一次在 Grails 中使用复选框。我想要复选框做的只是将一个布尔变量设置为 true(如果选中)。

来自form.gsp:

 <div class="form-group  col-md-12 ${hasErrors(bean: apiInstance, field: 'oneWay', 'error')} ">
 <label for="oneWay">
     <g:message code="api.oneway.label" default="One Way" />
 </label>
 <g:checkBox name="oneWay " value="true" checked="${apiInstance?.oneWay == 'true'}"/>
 </div>

在我的域类中,我定义了这个:

 Boolean oneWay = false

在从控制器调用的服务中,我检查 oneWay 的状态:

    if (apiInstance.oneWay == true) {
        log.info("One way flag is set.")
    } else {
        log.info("One way flag is not set.")
    }

即使我选中复选框,它也总是错误的。我犯了什么愚蠢的行为? :D

【问题讨论】:

  • ${apiInstance?.oneWay == true} 或简单的${apiInstance?.oneWay}
  • @SaschaFrinken 被称为String-driven development

标签: grails


【解决方案1】:

我认为您不需要使用 value 和 checked 属性,只需使用以下值:

<g:checkBox name="oneWay " value="${apiInstance?.oneWay}" />

【讨论】:

    猜你喜欢
    • 2015-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多