【问题标题】:grails button to do ajax call to change textboxgrails 按钮执行 ajax 调用以更改文本框
【发布时间】:2014-03-13 18:50:36
【问题描述】:

我的控制器中有一个方法

def returnJames = {
    JSONObject jsonObject = new JSONObject()
    jsonObject.put("name","James")
    return jsonObject
} 

然后我有一个看法:

<html>
<!--import gsp that has all the jquery imports-->
<script>
function changeName()
    {
        $.ajax({
            url:"<g:createLink url="[action:'returnJames',controller:'mycontroller']" />",
            dataType: "json",
            asnyc: false,
            success: function(data) {
                $('mytextbox').val(it.name)
            }
        });
    }
</script>
<g:form controller="mycontroller" method="post" >
<g:textField name="mytextbox" value="" />
<button name = "mybutton" id = "mybutton" onclick="changeName()">change name</button>
</g:form>
</html>

但是它只是试图将页面更改为不存在的 mycontroller 控制器的索引视图。我怎样才能让它只用“James”填写文本框?

【问题讨论】:

    标签: javascript jquery ajax grails gsp


    【解决方案1】:

    用户 render 而不是 return

    render jsonObject
    

    在 jQuery 中的变化

    success: function(data) {
        $('#mytextbox').val(data.name)
    }
    

    将按钮更改为

    <input type="button" id = "mybutton" onclick="changeName()" value="change name"/>
    

    【讨论】:

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