0

当用户单击删除时,我曾经能够使用下面的代码来隐藏一行,为什么这已经停止工作

$(document).ready(function () {
    $('.deleteRecord').live( function() {
        if(confirm("Are you sure?")){
            $(this).closest('tr').fadeOut();
        }
    });
});

哈姆线

\#{link_to image_tag('/img/icons/packs/diagona/16x16/101.png', :border => 0), schedule, :method => :delete, :remote=>true, :class=>'deleteRecord'}

这就是我在 application.js 中的内容,所有其他事件似乎都在工作或触发,减去这个

//= require jquery
//= require rails
//= require jquery_ujs
//= require jquery-ui
//= require best_in_place
//= require plugins

//= require messages
//= require_self


$(document).ready(function() {
    jQuery(".best_in_place").best_in_place();
    $('.best_in_place').bind("ajax:success", function(){
        $(this).closest('tr').effect("pulsate", { times:3 }, 500);
    });

});


$(window).load(function () {
    $('#tab-panel-1').createTabs();
    $('#dataTable').dataTable(
        {
            "aLengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
            "aaSorting": [[ 4, "desc" ]]
        }
    );
});

$(document).ready(function () {
    $('.deleteRecord').on('click', function() {
        if(confirm("Are you sure?")){
            $(this).closest('tr').fadeOut();
        }
    });

    $('#notice').effect("pulsate", { times:3 }, 500);



});
4

3 回答 3

1

试试这个:

    $(document).ready(function() {
        $('img.deleteRecord').on("click", function() {
            if ( confirm("Are you sure?") ) {
                $(this).closest('tr').fadeOut();
            }
        });
    });

.live()已弃用,.on()自 jQuery 1.7+ 起优先使用。

另外,只搜索类名也不好,如果可能的话,使用像上面这样的选择器。

编辑

如果行是动态添加的,则需要使用以下内容:

    $(document).ready(function() {
        $('#parent').on("click", "img.deleteRecord", function() {
            if ( confirm("Are you sure?") ) {
                $(this).closest('tr').fadeOut();
            }
        });
    });
于 2012-08-21T13:19:26.917 回答
1

我认为您滥用了该live功能:

$(document).ready(function() {
    $('.deleteRecord').live('click', function() {
        if(confirm("Are you sure?")){
            $(this).closest('tr').fadeOut();
        }
    });
});

现在,live已弃用,所以只需使用on

$(document).ready(function() {
    $('.deleteRecord').on('click', function() {
        if(confirm("Are you sure?")){
            $(this).closest('tr').fadeOut();
        }
    });
});

jQuery api .on()

编辑

事件处理程序仅绑定到当前选定的元素;当您的代码调用 .on() 时,它们必须存在于页面上。

的使用onlive取决于您的目的。

于 2012-08-21T13:19:32.567 回答
-1

描述:获取与选择器匹配的第一个元素,从当前元素开始,向上遍历 DOM 树。

从当前元素开始 向上遍历 DOM 树,直到找到与提供的选择器匹配的对象 返回的 jQuery 对象包含零个或一个元素

来源:http ://api.jquery.com/closest/

编辑

如果您完全理解文档,请使用 Firebug 控制台尝试您的代码。首先,试试这个:

$('.deleteRecord').eq(0).closest('tr').fadeOut();

如果有效,请尝试将此 onlick 函数添加到另一个元素,以查看它是否适用于它。

最后尝试使用确认()。

于 2012-08-21T13:24:19.133 回答