2012-07-10 27 views
9

**编辑此帖子是因为我发现问题确实在于无法绑定到ajax:success函数。jQuery绑定ajax:成功不适用于新创建的(ajax)项目的rails 3应用程序

***使用Rails 3.2.3

感谢您百忙之中阅读并设法帮助的时候。

我出的AJAX添加一个简单的淡入淡出功能:一个项目的成功被删除,具体如下:

$(document).ready(jQuery(function($) { 
    $('.delete').bind('ajax:success', function() { 
     $(this).closest('div').fadeOut(); 
    }); 
})); 
#For some reason had to pass the $ into the function to get it to work, 
#not sure why this is necessary but doesn't work unless that is done. 

在此之后,我想创建一个不显眼的jQuery对象时,用户添加的东西,并且在这些上也有工作删除按钮。我创建了一个create.js.erb文件,在创建新项目后调用 - 创建工作正常,但删除按钮无法访问ajax:success事件。

它确实从数据库中删除了点击数据,但.fadeOut永远不会绑定到它,所以它不会消​​失。该create.js.erb如下:

#$("div").append(html) here, then call the function below to try and bind the ajax:success function 
$('.delete').bind('ajax:complete', function() { 
    $(this).closest('div').fadeOut(); 
}); 

如果我改变了绑定到点击功能,它的工作原理,但这并不占失败的Ajax调用:

#$("div").append(html) here, then call the function below to try and bind the ajax:success function 
    $('.delete').bind('click', function() { 
     $(this).closest('div').fadeOut(); 
    }); 

所以,必须先绑定ajax:创建项目后成功。你们有什么想法,为什么这可能会发生?我是否需要在rails中做一些特殊的事情才能让新呈现的项目识别ajax事件?任何方向将不胜感激!

P.S.对于删除方法的控制器下面的情况下,任何人可以在那里发现了一个问题:

def destroy 
    @user = User.find(params[:user_id]) 
    @item = @user.item.find(params[:id]) 
    @item.destroy 

    respond_to do |format| 
     format.html { redirect_to user_path(@user) } 
     format.json { head :no_content } 
     format.js { render :nothing => true } 
    end 
end 
+0

这是一个Rails 3.0.x和3.1/3.2的应用程序? – ScottJShea 2012-07-10 05:46:43

+0

Rails 3.2.3是我正在使用的版本。这是非常可能的,我只是写错了代码,但是,因为你问了哪个版本的rails,我不得不问,你知道在早期版本的rails或类似的东西中有这样的错误吗? – Anthony 2012-07-15 02:11:54

+0

有时候人们会在同一个项目中激活jQuery和Prototype。 Rails 3.0.x更经常发生。现在我再看一遍,您可能想要将第二个bind('bind_delete_animation')移动到'document.ready'中,而不是从'document.ready'调用的函数中。我从来没有成功地接受过文档之外的束缚。 – ScottJShea 2012-07-15 03:31:35

回答

18

问题听起来像生成元素之前绑定正在发生的事情。试试这个:

$(document).on('ajax:success', '.delete', function(e) { 
    $(e.currentTarget).closest('div').fadeOut(); 
}); 
+0

谢谢!真棒 – 2015-10-09 15:58:58

12

我有同样的问题,因为我的表单处理程序方法返回一个普通字符串。一定要呈现jQuery可以处理的东西:

def handle_form 
    ... 
    render :json => {:what => 'ever'} 
    # or 
    render :nothing => true 
end 
+1

+1,这让我疯狂。非常感谢 – 2013-12-10 08:30:54

+1

同样对我来说 - 我渲染一个简单的字符串,它不会工作。非常感谢。 – user1581404 2016-02-18 15:22:23

1

我最近使用Rails 3.2.3做了类似的事情。我也用

$('#dialog-form').bind('ajax:success', function() { 
}) 

,它工作正常...

+1

其实我发现只要你在你的控制器中显示某些东西,ajax:success事件将不起作用。也许Rails也试图绑定到这一点。 – xiaodaidai 2013-08-15 20:32:35

0

检查,如果你通过捕捉ajax:completed得到一个解析错误(或类似)。在那里检查status参数。在我的情况下,这是一个parseerror由于mimetype不匹配。

您还可以在jquery_ujs.js中设置断点。

也看到这个问题jQuery ajax request not triggering JS response

1

尽管这是一个老问题,我有什么作品出来是同一个问题的答案更新。

从版本1.9开始,jQuery将不再考虑将空repsonse(render nothing:true)视为成功/完成。这意味着在你的控制器中你应该返回一个空的JSON对象。

respond_to do |format| format.json { render: {}.to_json } end

0

对我来说,那是因为我们使用的是苗条了,模板是“name.slim”。如果我们将它们更改为'name.slim.html',则ajax:success会正确触发。

我正在使用的解决方案是将格式添加到控制器中的渲染声明。

所以我们已经得到的东西是这样的:

render :new, layout: false, formats: [:html] 
0

即使它是一个老问题,没有一个答案真的帮了我,因为我有一个自定义的确认对话框和AJAX:成功是触发完全不同的元素。

为了找到哪些因素在这种情况下,你的Ajax trigered,你可以先使用代码:

$(document).on('ajax:success', 'a', function(evt, data, status, xhr) { 
    $(".log").text("Triggered ajaxComplete handler."); 
    // Check what is the value of $(this), and you will see what button has triggered the ajax success action, and you can start from there 
}); 
相关问题