我试图在表单中设置自动填充,因为每当id字段在表单中更改时,其他几个字段也会自动填充。我将jQuery用于on change事件。

这是id字段:

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

这是我的jQuery函数:
$(document).ready(function(){
        $("#id").change(function(){
            $("#otherField").change("${info.getOtherField('#id')}")
        });
     });

信息是我用来提取该字段所需信息的taglib。我无法弄清楚如何将字段中的数据提取到jquery语句内的常规代码中。
我正在使用Grails 3。

最佳答案

就像Joshua Moore所说的那样,没有ajax调用就无法向服务器端调用。这是一种方法:

$(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)
        }
    });


  });
});

您的 Controller 看起来像
MyController {
...
  def getTheOtherFieldValue(String id ) {
    render(contentType: 'text/json') {
      theOtherFieldValue = getOtherField(id)
    }
  }
...
}

基本上,触发器使用#id值对服务器进行ajax调用,等待其返回,然后使用返回的值更新#otherField

10-07 12:26
查看更多