简体   繁体   English

Grails的 <g:if> 在 <g:select>

[英]Grails <g:if> in <g:select>

I have this <g:select> in a .gsp file. 我在.gsp文件中有这个<g:select> But unlike any ordinary <g:select> 's this one would have the attribute disabled="" if a certain condition is met. 但是,与任何普通的<g:select>不同,如果满足某个条件,则该属性将具有disabled=""

Following the code: 遵循以下代码:

<g:select name="test" 
          from="${["foo1","foo2"]}" 
          <g:if test="${true}">disabled=""</g:if> />

It returned an error: Grails tag [g:select] was not closed 它返回了一个错误: Grails tag [g:select] was not closed

But when I change it into this: 但是当我把它改成这个:

<g:select name="test" 
          from="${["mu1","mu2","mu3"]}" 
          ${ if(true) { println "disabled=\"\"" } }/>

It returned this error: Attribute value must be quoted. 它返回了此错误: Attribute value must be quoted.

Both of the error message are under the exception, org.codehaus.groovy.grails.web.taglib.exceptions.GrailsTagException 这两个错误消息都在异常下, org.codehaus.groovy.grails.web.taglib.exceptions.GrailsTagException

The question is how could we make this work? 问题是我们如何才能使这项工作成功? Is there a possible answer without using a custom TagLib? 没有使用自定义TagLib,是否有可能的答案?

The GSP form field tags treat disabled as a boolean property, so you can say GSP表单字段标记将disabled视为布尔属性,因此您可以这么说

<g:select .... disabled="${true}" />

Generally you should be able to use any expression under the usual Groovy-truth rules but I believe it makes a special case for the strings "true" and "false" (the latter would normally be considered true under Groovy-truth rules as a non-empty string). 一般来说,你应该能够使用按照通常的Groovy的真相规则的表达,但我相信,这使该字符串的特殊情况“真”与“假”(后者通常被认为是true在Groovy的真相规则作为非-empty string)。 If in doubt you can always say 如果有疑问你可以随时说

disabled="${(someExpression) as boolean}"

无需使用println,试试这个

<g:select .... ${(conditional)?"disabled":""} ... />
    <g:select disabled="${true}"...

is fine but when you submit and it is a required field the value will not be submitted so use this jQuery code to enable the field when pressing the submit button 没问题,但是当你提交并且它是必填字段时,该值将不会被提交,因此在按下提交按钮时使用此jQuery代码启用该字段

    $(function() {

        $('form').on('submit', function() {
            $(this).find(':disabled').removeAttr('disabled');
        });

    });

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

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