簡體   English   中英

Grails通過groovy子句中的id獲取字段值

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

我試圖在表單中設置自動填充,因為每當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)    
        }
    });


  });
});

您的控制器看起來像

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

基本上,觸發器使用#id值對服務器進行ajax調用,等待其返回,然后使用返回的值更新#otherField

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM