【问题标题】:load a gif image while the deleting an image via ajax通过 ajax 删除图像时加载 gif 图像
【发布时间】:2013-02-20 21:41:47
【问题描述】:

我有一个用户上传图片的页面。每个图像都分配有一个删除按钮。我正在尝试实现一种方法,当单击图像下方的删除按钮时,该特定的删除按钮消失并在加载 ajax 请求时加载 gif 图像,并且刷新完成后 gif 图像应该消失。 这是正在尝试的方法,但它不起作用。

$(function(){
    $('.delete-img-btn').live('click', function() {
        //asign the image id from the button attribute 'img-id'
        var id= $(this).attr('img-id');
        //The data to be send via ajax the server will recieve 2 POST variables ie. 'action' and 'id'(which is the img id)
        var data={
            'action':'/admin/image-uploader/',
                'pk':id,
           'success':refreshUploadedImages
        };

        $('.inner').empty();
        $('.inner').append('<img src="{{ MEDIA_URL }}admin/img/admin/ajax-loader.gif" id="spinner">');
        //inherit parent element (our delete button)
        $(this).ajaxStart(function() {
            $('#spinner').show();
        }).ajaxStop(function() {
            $('#spinner').hide();
        });
        $.post("/admin/image-uploader/delete/"+id ,function(data){
            refreshUploadedImages()
        });
    });
});

【问题讨论】:

    标签: ajax jquery django-templates


    【解决方案1】:

    您不需要ajaxStartajaxStop...在$.post 之前显示微调器并在$.post 完成后隐藏它(回调函数)。

    $('.inner').empty();
    $('.inner').append('<img src="{{ MEDIA_URL }}admin/img/admin/ajax-loader.gif" id="spinner">');  //show here
    $.post("/admin/image-uploader/delete/"+id ,function(data){
        $('#spinner').hide();  //hide here
        refreshUploadedImages()
    });
    

    【讨论】:

    • 这是所有的按钮,甚至没有加载 gif。我希望它只隐藏图像下的按钮..并加载 gif ..
    • 你也可以发布你的html吗..这样我就可以看到结构并同样修改因为假设我自己很难回答你的问题.. :)
    猜你喜欢
    • 1970-01-01
    • 2012-06-05
    • 2011-05-25
    • 2016-02-15
    • 2019-01-14
    • 1970-01-01
    • 1970-01-01
    • 2011-03-09
    • 1970-01-01
    相关资源
    最近更新 更多