Rails 3 - 在弹出窗口中渲染部分 ajax 调用
我有一个带有remote=>true的按钮,它通过以下方式调用弹出窗口(jquery弹出窗口,不是真正的弹出窗口):
$modal = $('#modal')
$modal_close = $modal.find('.close')
$modal_container = $('#modal-container')
$task_select_div = $('.activity_task_add')
# Handle modal links with the data-remote attribute
$('a[data-remote]').on 'ajax:success', (xhr, data, status) ->
$modal
.html(data)
.prepend($modal_close)
.css('top', $(window).scrollTop() + 150)
.show()
#This is the callback that is not being executed.
$('form[data-remote]').on 'ajax:success', (xhr, data, status) ->
alert(data)
$modal_container.hide()
$modal.hide()
$task_select_div.html(data)
在该弹出窗口中,我在该表单的提交按钮中有另一个带有remote_tag的表单,我调用并执行该操作底部有以下代码:
respond_to do |format|
if @task.save
format.html { redirect_to @task, notice: 'Task was successfully created.' }
format.json { render json: @task, status: :created, location: @task }
format.js {render :partial => 'tasks', :locals => {:tasks => current_user.department.tasks}}
else
format.html { render action: "new" }
format.json { render json: @task.errors, status: :unprocessable_entity }
end
end
它执行 format.js 并且控制台显示“Rendered jobs/_tasks.html.erb (5.8ms)”,但 ajax 调用的回调不起作用。
$('form[data-remote]').on 'ajax:success', (xhr, data, status) ->
alert(data)
我需要接收 ajax:success 事件才能隐藏弹出窗口。 有什么帮助吗?
I have a button with remote=>true that calls a popup (a jquery popup, not a real one) in the following way:
$modal = $('#modal')
$modal_close = $modal.find('.close')
$modal_container = $('#modal-container')
$task_select_div = $('.activity_task_add')
# Handle modal links with the data-remote attribute
$('a[data-remote]').on 'ajax:success', (xhr, data, status) ->
$modal
.html(data)
.prepend($modal_close)
.css('top', $(window).scrollTop() + 150)
.show()
#This is the callback that is not being executed.
$('form[data-remote]').on 'ajax:success', (xhr, data, status) ->
alert(data)
$modal_container.hide()
$modal.hide()
$task_select_div.html(data)
In that popup I have another form with remote_tag in the submit button of this form I call and action that has the following code at the bottom:
respond_to do |format|
if @task.save
format.html { redirect_to @task, notice: 'Task was successfully created.' }
format.json { render json: @task, status: :created, location: @task }
format.js {render :partial => 'tasks', :locals => {:tasks => current_user.department.tasks}}
else
format.html { render action: "new" }
format.json { render json: @task.errors, status: :unprocessable_entity }
end
end
It executes format.js and the console says "Rendered tasks/_tasks.html.erb (5.8ms)" but the callback for the ajax call is not working.
$('form[data-remote]').on 'ajax:success', (xhr, data, status) ->
alert(data)
I need to receive an ajax:success event in order to hide the Popup.
Any help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
删除这一行:
更新你的 js 回调:
Remove this line :
Update your js callback :
解决了。
改变了我的response_to do|格式|对于这个:
我的 javascript 对于这个:
你觉得这个解决方案怎么样?有什么缺点吗?
Solved it.
Changed my respond_to do |format| for this:
And my javascript for this:
What do you think of this solution? Any drawbacks?