-4

我想转换.live为使用不多的.on方法,.live但我无法转换它。请帮我改一下。

$(".delete").live('click', function () {
    var id = $(this).attr('id');
    var b = $(this).parent().parent();
    var dataString = 'id=' + id;
    if (confirm("Sure you want to delete this update?")) {
        $.ajax({
            type: "POST",
            url: "delete.php",
            data: dataString,
            cache: false,
            success: function (e) {
                b.hide();
                e.stopImmediatePropagation();
            }
        });
        return false;
    }
});
4

3 回答 3

1

改变:

$(".delete").live('click',function()

至:

$("body").on('click', '.delete', function () {

解决方案:

$("body").on('click', '.delete', function () {
    var id = $(this).attr('id');
    var b = $(this).parent().parent();
    var dataString = 'id=' + id;
    if (confirm("Sure you want to delete this update?")) {
        $.ajax({
            type: "POST",
            url: "delete.php",
            data: dataString,
            cache: false,
            success: function (e) {
                b.hide();
                e.stopImmediatePropagation();
            }
        });
        return false;
    }
});

另请参阅jQuery 1.9 .live() 不是函数jQuery - 如何使用“on()”方法而不是“live()”?

于 2013-04-15T06:44:23.597 回答
1
$(".delete").live('click',function()

可以转换为

$(document).on('click', ".delete", function()

文档

于 2013-04-15T06:35:18.007 回答
0
$("#someArbitraryContextNode").on('click',".delete",function(){});
于 2013-04-15T06:35:59.607 回答