简体   繁体   English

Grails通过groovy子句中的id获取字段值

[英]Grails get field value by id in groovy clause

I'm trying to set autofill in a form in that whenever the id field changes in the form, several other fields would autofill. 我试图在表单中设置自动填充,因为每当id字段在表单中更改时,其他几个字段也会自动填充。 I am using jQuery for the on change event. 我将jQuery用于on change事件。

This is the id field: 这是id字段:

<g:field id="id" name="id" value="${this.myContoller?.id}"/>

And this is my jQuery function: 这是我的jQuery函数:

$(document).ready(function(){
        $("#id").change(function(){
            $("#otherField").change("${info.getOtherField('#id')}")
        });
     });

Info is a taglib that I am using to pull the info I need for that field. 信息是我用于提取该字段所需信息的taglib。 I cannot figure out how to pull the data from the field into the groovy code inside of the jquery statement. 我无法弄清楚如何将字段中的数据提取到jquery语句内的常规代码中。 I am using Grails 3. 我正在使用Grails 3。

Like Joshua Moore said you can't callout to the server side without as ajax call. 就像Joshua Moore所说的那样,没有ajax调用就无法向服务器端调用。 Here's one way of doing that: 这是一种方法:

$(document).ready(function(){
  $("#id").change(function(){
    $.ajax({
        url: "${createLink(controller: 'myController', action: 'getTheOtherFieldValue')}",
        data: {
          'id' : $(this).val()
        },
        success: function(data, textStatus) {
          $("#otherField").change(data.theOtherFieldValue)    
        }
    });


  });
});

And your controller can look like, 您的控制器看起来像

MyController {
...
  def getTheOtherFieldValue(String id ) {
    render(contentType: 'text/json') {
      theOtherFieldValue = getOtherField(id)
    }
  }
...
}

Basically, your trigger makes an ajax call to the server with the #id values, waits till it returns and then updates #otherField with the returned value(s) 基本上,触发器使用#id值对服务器进行ajax调用,等待其返回,然后使用返回的值更新#otherField

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

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