Grails使用ajax提交表单 [英] Grails using ajax submit a form

查看:131
本文介绍了Grails使用ajax提交表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在grails中与ajax相关的问题堆叠,请帮忙!
在域名簿中,我得到了书名和书本类型,然后我在该域上生成了一个控制器并查看基础。然后在 bookController 的更新操作中,我使用jquery弹出输入书名和书本类型,然后使用ajax技术更新该书。我希望你得到了我。
在这里我的代码:

更新函数

  def update(Long id,Long version){
def bookInstance = Book.get(id)
if(!bookInstance){
flash.message = message(code:'default.not。 found.message',
args:[message(code:'book.label',default:'Book'),id])
redirect(action:list)
return $ (版本!= null){
if(bookInstance.version>版本){
bookInstance.errors.rejectValue(version,default .optimistic.locking.failure,
[message(code:'book.label',default:'Book')] as Object [],
另一位用户在编辑时更新了本书)

render(view:edit,model:[bookInstance:bookInstance])
return
}
}

bookInstance .properties = params

if(!bookInstance.save(flus h:true)){
render(view:edit,model:[bookInstance:bookInstance])
return
}

flash.message = message(代码:'default.updated.message',
args:[message(code:'book.label',default:'Book'),bookInstance.id])
redirect(action:show ,id:bookInstance.id)
}

这里是文本框

 < div id =dialogtitle =编辑书籍style =display:none> 
< form id =ajaxForm>
书名:< input type =textname =name>< br>
图书类型:< input type =textname =type>< br>< br>
< input type =submitvalue =Ok>
< / form>
< / div>

这就是我遇到的麻烦。

 < script> 
函数showDialog(){
$(#dialog).dialog()
}
函数getRequest(){
$(#ajaxForm)。 form({
type:'POST'
url:'/ test / book / update?name =& type ='
})
}
< /脚本>

请帮我完成ajax代码。

 < g:formRemote name =myFormurl =[controller:'book',action:'update' ]> 
书名:< input type =textname =name>< br>
图书类型:< input type =textname =type>< br>< br>
< input type =submitvalue =Ok>
< / g:formRemote>


I'm stacking with issues related to ajax in grails, please help! In domain book, I got book name and book type, then I generated a controller and view base on that domain. Then in update action of bookController, I use jquery to pop up input book name and book type, then I used ajax technique to update that book. I hope you got me. Here my code:

For Update function

def update(Long id, Long version) {
    def bookInstance = Book.get(id)
    if (!bookInstance) {
    flash.message = message(code: 'default.not.found.message', 
                    args: [message(code: 'book.label', default: 'Book'), id])
            redirect(action: "list")
            return
     }

    if (version != null) {
       if (bookInstance.version > version) {
      bookInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
      [message(code: 'book.label', default: 'Book')] as Object[],
       "Another user has updated this Book while you were editing")

            render(view: "edit", model: [bookInstance: bookInstance])
            return
            }
        }

      bookInstance.properties = params

     if (!bookInstance.save(flush: true)) {
          render(view: "edit", model: [bookInstance: bookInstance])
          return
     }

    flash.message = message(code: 'default.updated.message', 
    args: [message(code: 'book.label', default: 'Book'), bookInstance.id])
        redirect(action: "show", id: bookInstance.id)
    }

here is the text box

<div id="dialog" title="Edit book" style="display: none">
        <form id="ajaxForm">
            Book name: <input type="text" name="name"><br>
            Book type: <input type="text" name="type"><br><br>
            <input type="submit" value="Ok">
        </form>
</div>

And this is what I faced with trouble.

     <script>
            function showDialog() {
                $( "#dialog" ).dialog()
            }
            function getRequest(){
                $("#ajaxForm").form({
                    type: 'POST'
                    url: '/test/book/update?name=&type='
                })
            }
     </script>

Please help me to finish ajax code. Thanks for advance.

解决方案

Try replacing your <form> with this

<g:formRemote name="myForm" url="[controller: 'book', action: 'update']">
    Book name: <input type="text" name="name"><br>
    Book type: <input type="text" name="type"><br><br>
    <input type="submit" value="Ok">
</g:formRemote>

这篇关于Grails使用ajax提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆