jQuery的绑定阿贾克斯:成功不是工作在轨道3应用程序的新创建(阿贾克斯)项目 [英] jQuery bind ajax:success not working in rails 3 app for newly created (ajax) items

查看:167
本文介绍了jQuery的绑定阿贾克斯:成功不是工作在轨道3应用程序的新创建(阿贾克斯)项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

**编辑这个职位,因为我发现这个问题确实是在铁轨无法绑定到AJAX:成功的功能

***使用Rails 3.2.3

感谢您抽出时间来阅读和尽力帮助。

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

  $(文件)。就绪(jQuery的(功能($){
    $('删除')绑定(AJAX:成功',函数(){
        $(本).closest('格')淡出();
    });
}));
#For某种原因,不得不通过$到函数来得到它的工作,
#not知道为什么这是必要的,但不能正常工作,除非做到这一点。
 

在此,我想用不显眼的jQuery创建一个对象,当用户增加了一些,并有工作删除这些按钮以及。我所做的是创建一个新的项目后称为create.js.erb文件 - 和创造工作正常,但删除按钮无法访问AJAX:成功事件

有确实点击时从分贝删除数据,但.fadeOut从未绑定到它,因此它不消失。的create.js.erb如下:

 #$(格)追加(HTML)在这里,然后调用下面的函数来尝试并绑定了ajax:成功的功能
$('删除')绑定(AJAX:完成,函数(){
    $(本).closest('格')淡出();
});
 

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

 #$(格)追加(HTML)在这里,然后调用下面的函数来尝试并绑定了ajax:成功的功能
    $('。删除)。绑定(点击,函数(){
        $(本).closest('格')淡出();
    });
 

所以东西一定要与绑定到AJAX:在创建项目之后的成功。你们有什么想法,为什么这可能发生?我需要做的栏杆一些特别的东西来获得新渲染的项目识别AJAX事件?任何方向将是非常美联社preciated!

P.S。控制器的删除方法是下面的情况下任何可能能够检测一个问题存在:

  DEF破坏
    @user = User.find(PARAMS [:USER_ID])
    @item = @ user.item.find(PARAMS [:ID])
    @ item.destroy

    respond_to代码做|格式|
      的format.html {redirect_to时user_path(@user)}
      format.json {头:} NO_CONTENT
      format.js {渲染:没有=>真正 }
    结束
结束
 

解决方案

这个问题听起来就像是绑定发生生成元素之前。试试这个:

  $(文件)。在('阿贾克斯:成功','.delete',功能(五){
    $(e.currentTarget).closest('格')淡出();
});
 

**Edited this post because I found the problem is really in the inability of rails to bind to the ajax:success function.

***Using rails 3.2.3

Thanks for taking the time to read and try to help.

I am adding a simple fade out function on the ajax:success of an item being deleted, as follows:

$(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.

After this, I wanted to create an object with unobtrusive jQuery when the user adds something, and have working delete buttons on those as well. I made a create.js.erb file that is called after creating a new item - and the creation works fine, but the delete button cannot access the ajax:success event.

It does delete the data from the db when clicked, but the .fadeOut is never bound to it, so it doesn't disappear. The create.js.erb is as follows:

#$("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();
});

If I change this to bind to the click function, it works, but this doesn't account for a failed ajax call:

#$("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();
    });

So something must be up with binding to the ajax:success after the item is created. You guys have any idea why this might be happening? Do I need to do something special in rails to get the newly rendered item to recognize ajax events? Any direction would be much appreciated!

P.S. The controller for the delete method is below in case anyone might be able to detect a problem there:

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

解决方案

The problem sounds like the bind is happening before the element is generated. Try this:

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

这篇关于jQuery的绑定阿贾克斯:成功不是工作在轨道3应用程序的新创建(阿贾克斯)项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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