簡體   English   中英

為jQuery-File-Upload實現刪除按鈕

[英]Implementing Remove Buttons for jQuery-File-Upload

我可能會以完全錯誤的想法接近這個,所以讓我先解釋一下我的情況。

我想用jQuery-File-Upload實現的目的是允許用戶選擇他們選擇上傳的文件。 他們將被允許選擇多個文件。 目前無論什么類型都沒關系,但它們主要是上傳圖片。 所選文件將顯示在列表中,每個文件的文件名右側都有一個“刪除”按鈕。 如下圖所示。

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

我確實意識到這可能是這篇文章的重新發布,但這是兩個問題,在這里我認為我把它細分為更小的問題。

通過點擊事件上傳中止

首先,我需要告訴你,我已經建立了類似於你的東西。 達到這一點后,我問自己同樣的問題,最后到此為止。 不幸的是,我不得不幫助自己並解決了它。

您的基本問題是您正在使用方法簽名來執行click事件( function(e, data) ),該事件將覆蓋包含add方法( function(e, data) )的簽名。 因此,在您嘗試刪除數據上下文時,您實際上正在更改click事件的數據。 因此,只需在click事件中保留變量(或重命名它們),因為在您的示例中您不需要它們。

在這之后你最終試圖干凈地“破壞”上傳。 我想通了,首先中止它們並禁用任何后續提交都很干凈。

這是一個適合您需求的工作代碼示例,並且有一些不言自明的注釋:

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-events和jQuery之間進行另一種選擇。 您應該打開另一個問題或嘗試找到相關的問題。 在這樣做時,我同意將腳本放在一個干凈的匿名函數中,該函數在文檔准備好后自動加載: $(function() { /*yourcontent*/ });

根據插件的文檔,應該有一個.destroy方法,但是文檔對於如何初始化它,或者它是從數組中銷毀文件,還是只是破壞整個表單都非常模糊。 應該有一種方法,至少在理論上應該有。

至於第二部分,是的。 您的代碼應該在jQuery初始化程序中,例如$(document).ready或更受歡迎的$(function() {});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM