【问题标题】:Implementing Remove Buttons for jQuery-File-Upload为 jQuery-File-Upload 实现删除按钮
【发布时间】:2014-12-15 03:24:24
【问题描述】:

我可能以一个完全错误的想法来处理这个问题,所以让我先解释一下我的情况。

我试图使用 jQuery-File-Upload 实现的是允许用户选择他们选择上传的文件。他们将被允许选择多个文件。现在不管是什么类型,但他们主要是上传图片。选定的文件将出现在一个列表中,每个文件的文件名右侧都有一个“删除”按钮。如下图所示。

如何实现允许用户从等待上传的文件列表中删除他们选择的文件?

一旦用户对他们选择的文件感到满意,他们将单击“开始上传”按钮,该按钮将开始上传列表中包含的文件并触发上图所示的进度条。

我自己尝试了一些故障排除,包括注销单击按钮时发生的事件,但无法将其从选择上传的文件列表中删除。我知道有一个drop 回调,但我不确定这是否是我应该使用的。

下面是我的代码。

$(function () {
    $('#fileupload').fileupload({
        dataType: 'json',
        url: '../php/',
        add: function (e, data) {
            //$.each(data.files, function(index, file) {
                data.context = $('<li class=\"list-group-item\">')
                    //.html(file.name+"<button type=\"button\" id=\"drop\" class=\"btn btn-danger btn-xs pull-right\"><span class=\"glyphicon glyphicon-remove\"></span></button>")
                    // see https://stackoverflow.com/questions/26234279/blueimp-jquery-upload-multiple-files-doesnt-work for the reason for the line below
                    .html(data.files[0].name+"<button type=\"button\" id=\"drop\" class=\"btn btn-danger btn-xs pull-right\"><span class=\"glyphicon glyphicon-trash\"></span></button>")
                    .appendTo(".list-group");
                /*$('.btn-danger').on('click', function() {
                    console.log('Drop '+file.name+' \n');
                });*/
            //});
            $('.btn-danger').on('click', function(e, data) {
                //console.log("Removing all objects...\n");
                //data.context.remove(data.files[0].name);
                //data.context.remove(data.files[0]);
                e.preventDefault();
                /*var filename = $(this).parent().text();
                console.log("Filename: "+filename);
                data.context.remove(data.files.filename);
                console.log("Clicked the drop button");*/
                try { // here I try thinking that I can call the drop() callback but I'm still trying to wrap my head around how this all works.
                    drop(e, data);
                } catch(error) {
                    console.log("The error was: "+error);
                }
            });


        },
        submit: function (e, data) {
            $('#start-upload').on('click', function() {
                //$('#start-upload').addClass('#disabledInput');
                console.log("This is the start upload button!");
            });
        },
        done: function (e, data) {
            /*$.each(data.result.files, function (index, file) {
                $('<p/>').text(file.name).appendTo('.files').find('#panel-body');
            });*/
        },
        progressall: function (e, data) {
            var progress = parseInt(data.loaded / data.total * 100, 10);
            $('#progress .bar').css(
                'width',
                progress + '%'
            );
        },
        drop: function (e, data) {
            $.each(data.files, function (index, file) {
                //$('.btn-danger').on('click', function() {
                    console.log('Dropped file: '+ file.name +'\n');
                //});
            });
        }
    }).on('.fileuploadsubmit', 'click', function(e, data) {
        console.log("Submit button pressed.");
        //data.formData = data.context.find(':input').seralizeArray();
    });
});

最后,所有这些都应该放在我的$(document).ready 中吗?

我确实意识到这可能是 this post 的重新发布,但这是两个问题合而为一,我认为我将其细分为更小的问题。

【问题讨论】:

    标签: javascript jquery file-upload jquery-file-upload blueimp


    【解决方案1】:

    点击事件中止上传

    首先我需要告诉你,我已经建立了与你类似的东西。达到那一点后,我问了自己同样的问题并最终来到了这里。不幸的是,我不得不帮助自己并解决了它。

    您的基本问题是您正在为单击事件 (function(e, data)) 使用方法签名,这将覆盖包含添加方法 (function(e, data)) 的签名。因此,当您尝试删除数据上下文时,您实际上是在更改点击事件的数据。因此,只需将变量(或重命名)留在点击事件中,因为在您的示例中您不需要它们。

    这样做之后,您最终会尝试干净地“销毁”上传。我发现,首先中止它们并禁用以后的提交是很干净的。

    这是一个可以满足您需求并具有一些不言自明的 cmets 的工作代码示例:

    add: function (e, data) {
    
    // Append data context to collection.
    data.context = $('<li class="list-group-item">'+data.files[0].name+'<button class="btn btn-danger btn-xs pull-right"><i class="glyphicon glyphicon-trash"></i></button>')
        .appendTo('.list-group');
    
    // Search in the children of the data context for the button
    // and register the click event.
    data.context.children('button')
        .click(function () {
            // This button will be disabled.
            $(this).addClass('disabled');
            // Abort the data upload if it's running.
            data.abort();
            // Overwrite the plugin's default submit function.
            data.submit = function () { return false; };
            // Optional: Remove the data context (thus from collection).
            //data.context.remove();
            })
        ;
    
    },
    

    附加提示:不要对 JavaScript 使用双引号,因为您需要在 HTML 标记中转义它们。

    如何加载 JavaScript

    这个问题比你想象的要复杂得多。这是关于一致性/干净的 HTML 标记与页面加载的永无止境的讨论。选择一个方向时,您最终会在onload-evention和jQuery之间进行另一种选择。您应该打开另一个问题或尝试查找相关问题。这样做时,我同意将您的脚本放在一个干净的匿名函数中,该函数会在文档准备好后自动加载:$(function() { /*yourcontent*/ });

    【讨论】:

    • 谢谢,我会试试这个,如果可行,请将您的答案作为解决方案!
    • 感谢您的反馈。 ;-)
    • 正在处理它。对不起!
    【解决方案2】:

    根据插件的文档,应该有一个 .destroy 方法,但是文档对于如何初始化它,或者它是否从数组中销毁文件,或者只是销毁整个表单非常模糊。应该有办法,至少理论上应该有。

    至于第二部分,是的。您的代码应该在 jQuery 初始化程序中,例如 $(document).ready 或更流行的 $(function() {});

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-02
      • 2013-09-26
      • 2017-05-22
      • 2012-05-11
      • 1970-01-01
      • 2015-02-18
      • 2014-05-28
      相关资源
      最近更新 更多