使用 jQuery 绑定,顺序错误?

发布于 2024-10-17 02:18:28 字数 1153 浏览 2 评论 0原文

我有一个加载内容的脚本,然后应该绑定删除链接以删除内容。

但是我发现它没有与删除链接绑定,即使我将代码放入应该重新绑定的函数中。

$(document).ready(function() {


    function loadImages() {
        $.get('/management/image/get/site_id/1/area/News', function(data) {
            $("#existing-images").html(data);
        });

        $(".deleteImage").click(function() {
            id = $(this).attr('id'); id = id.split('_');
            alert(id);
            /*$.post(second_url+'id/'+id, '', function(theResponse) {
                $("#image_"+id+"").remove();
            });*/
        });
    }


    $("#fileInput").uploadify({
        'uploader'       : '/library/jquery/uploadify/uploadify.swf',
        'script'         : '/gallery/image/area/site_id/<?php echo $site->id; ?>/area/<?php echo $this->area; ?>',
        'cancelImg'      : '/library/jquery/uploadify/cancel.png',
        'folder'         : '/images/Image/',
        'multi'          : true,
        'onAllComplete'  : function(e, queueId, file, response, data) {
            $('#fileInput').uploadifyClearQueue();
            loadImages();
        },
    });


    loadImages();


});

I have this script that loads in content, and should then bind the delete link to remove the content.

However I'm finding that it isnt binding with the delete link, even then I put the code in a function that should rebind.

$(document).ready(function() {


    function loadImages() {
        $.get('/management/image/get/site_id/1/area/News', function(data) {
            $("#existing-images").html(data);
        });

        $(".deleteImage").click(function() {
            id = $(this).attr('id'); id = id.split('_');
            alert(id);
            /*$.post(second_url+'id/'+id, '', function(theResponse) {
                $("#image_"+id+"").remove();
            });*/
        });
    }


    $("#fileInput").uploadify({
        'uploader'       : '/library/jquery/uploadify/uploadify.swf',
        'script'         : '/gallery/image/area/site_id/<?php echo $site->id; ?>/area/<?php echo $this->area; ?>',
        'cancelImg'      : '/library/jquery/uploadify/cancel.png',
        'folder'         : '/images/Image/',
        'multi'          : true,
        'onAllComplete'  : function(e, queueId, file, response, data) {
            $('#fileInput').uploadifyClearQueue();
            loadImages();
        },
    });


    loadImages();


});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

似狗非友 2024-10-24 02:18:28

您可以使用 $.live 函数动态绑定删除链接,而不是每次创建时都这样做要求。

尝试类似的东西:

//..init code
$(".deleteImage").live('click',function() {
        id = $(this).attr('id'); id = id.split('_');
        alert(id);
});

function loadImages() {
    $.get('/management/image/get/site_id/1/area/News', function(data) {
            $("#existing-images").html(data);
    });
}

//more code here...

You may use the $.live function to dynamically bind delete links instead of doing it every time you make an request.

try something like:

//..init code
$(".deleteImage").live('click',function() {
        id = $(this).attr('id'); id = id.split('_');
        alert(id);
});

function loadImages() {
    $.get('/management/image/get/site_id/1/area/News', function(data) {
            $("#existing-images").html(data);
    });
}

//more code here...
扎心 2024-10-24 02:18:28

您正在 ajax 请求完成之前绑定删除链接。

you are binding the delete link before ajax request complete.

不必在意 2024-10-24 02:18:28

这是因为 ajax 调用的异步性质。

您编写的代码尝试将单击事件绑定到尚未注入 DOM 的按钮

尝试将代码更改为:

$(document).ready(function() {


    function loadImages() {
        $.get('/management/image/get/site_id/1/area/News', function(data) {
            $("#existing-images").html(data);

            $(".deleteImage").click(function() {
                id = $(this).attr('id'); id = id.split('_');
                alert(id);
                /*$.post(second_url+'id/'+id, '', function(theResponse) {
                    $("#image_"+id+"").remove();
                });*/
            });
        });


    }



    $("#fileInput").uploadify({
        'uploader'       : '/library/jquery/uploadify/uploadify.swf',
        'script'         : '/gallery/image/area/site_id/<?php echo $site->id; ?>/area/<?php echo $this->area; ?>',
        'cancelImg'      : '/library/jquery/uploadify/cancel.png',
        'folder'         : '/images/Image/',
        'multi'          : true,
        'onAllComplete'  : function(e, queueId, file, response, data) {
            $('#fileInput').uploadifyClearQueue();
            loadImages();
        },
    });


    loadImages();
});

将绑定移动到 $.get 调用的成功回调中使得确保在绑定发生之前已注入新的 html。

或者尝试使用 delegate 而不是绑定到元素,这样就不需要重新绑定一直

It's because of the asynchronous nature of the ajax call.

The code you have writen tries to bind the click event to a button that has not yet been injected into the DOM

Try changing your code to:

$(document).ready(function() {


    function loadImages() {
        $.get('/management/image/get/site_id/1/area/News', function(data) {
            $("#existing-images").html(data);

            $(".deleteImage").click(function() {
                id = $(this).attr('id'); id = id.split('_');
                alert(id);
                /*$.post(second_url+'id/'+id, '', function(theResponse) {
                    $("#image_"+id+"").remove();
                });*/
            });
        });


    }



    $("#fileInput").uploadify({
        'uploader'       : '/library/jquery/uploadify/uploadify.swf',
        'script'         : '/gallery/image/area/site_id/<?php echo $site->id; ?>/area/<?php echo $this->area; ?>',
        'cancelImg'      : '/library/jquery/uploadify/cancel.png',
        'folder'         : '/images/Image/',
        'multi'          : true,
        'onAllComplete'  : function(e, queueId, file, response, data) {
            $('#fileInput').uploadifyClearQueue();
            loadImages();
        },
    });


    loadImages();
});

Moving the binding into the success callback of the $.get call makes sure that the new html has been injected before the binding occurs.

Alternativly try looking into using delegate instead of binding to the element, that way you don't need to rebind all the time

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文