我有一个包含项目列表的页面,来自Rails后端.我希望能够通过Rails UJS使用Ajax调用来编辑该列表中的行.
途径
我在每行的末尾添加了一个编辑按钮.编辑按钮是一个
link_to …:remote =>真正.单击它会再次加载列表,但是会选中
在编辑模式下行.可编辑的行嵌入在表单中……:remote =>真正.
该行中的保存按钮是一个提交按钮.
index.html.haml
#editor %table - @items.each do |item| %tr = render :partial => 'row',:locals => { :item => item }
_row.html.haml
... %td // a number of columns with attributes ... %td = link_to t("actions.edit"),edit_item_path(item),:remote => true = link_to t("actions.delete"),item_path(item),:remote => true,:method => :delete,:data => { :confirm => "Are you sure?" }
edit.html.haml
#editor %table - @items.each do |item| %tr - if item == @item = form_for @item,:url => item_path(@item),do |f| = render :partial => "row_form",:locals => { :f => f } - else = render :partial => 'row',:locals => { :item => item }
... %td // a number of columns with editable attributes ... %td %button{ :type => "submit" }=t("actions.save") = link_to t("actions.cancel"),items_path,:remote => true
Ajax响应处理
$("#editor").on("ajax:success",function(event,data,status,xhr) { $("#editor").html($(data).find("#editor").html()); });
问题
当我在编辑模式/ items / 12 / edit中加载列表页面时,项目12的行是
编辑.单击保存按钮正确地通过Ajax提交表单并加载
/ items索引部分,用jQuery替换可编辑列表.点击
再次编辑按钮,再次加载编辑页面(例如/ items / 12 / edit)
嵌入式表格.只有这一次,表单不再提交了
单击保存按钮.似乎提交事件处理程序没有附加到
动态加载远程表单.
题
如何通过Ajax提交加载的远程表单,最好使用Rails UJS方法?
重复
我知道这个问题有重复,但没有一个得到回答.我希望有人最终得出一个明确的答案.
> Rails remote form loaded via Ajax after document ready is not submitting
> :remote => true/data-remote on a form loaded via ajax
因此,只要您使用javascript更改DOM,就应该重新初始化此回调
$("#editor").on("ajax:success",xhr) { $("#editor").html($(data).find("#editor").html()); });
前段时间jQuery的功能叫做“live”.它像“开启”一样工作,但跟踪了DOM的变化.但是在当前版本的jQuery中,这个函数已被弃用,因为它很慢.
希望你能理解我的英语不好=)
以上是大佬教程为你收集整理的jquery – Rails:无法提交通过Ajax加载的远程表单全部内容,希望文章能够帮你解决jquery – Rails:无法提交通过Ajax加载的远程表单所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。