2017-04-03 72 views
0

我真的很困惑,爲什麼會發生這種情況。我爲我的表單創建了一個拖放區域,到目前爲止,我可以刪除這些文件並報告已刪除的內容。然後,我試圖在提交之前附加拖放文件,然後通過ajax提交發送表單。使用jQuery刪除文件後沒有添加文件

我可以刪除文件,但他們似乎並沒有提交表格。

 // Flie Input Listener 
    .on('change', 'input[type="file"]', function(e) { 
    showFiles(e.target.files); 
    droppedFiles = ""; 
    //$form.trigger('submit'); // automatically submit the form on file select 
    }) 
    .on('drag dragstart dragend dragover dragenter dragleave drop', '#dropArea', function(e) { 
    e.preventDefault(); 
    e.stopPropagation(); 
    }) 
    .on('dragover dragenter', '#dropArea', function() { 
    $("#dropArea").addClass('is-dragover'); 
    }) 
    .on('dragleave dragend drop', '#dropArea', function() { 
    $("#dropArea").removeClass('is-dragover'); 
    }).on('drop', '#dropArea', function(e) { 
     droppedFiles = e.originalEvent.dataTransfer.files; 
     showFiles(droppedFiles); // function to replace text with file name dropped 
     var $form = $('#dropArea').closest('form'); find the form the files were dropped on 
     console.log($form.attr('id')); // check the form was found 
     var files = new FormData($form.get(0)); // create the Form Data object to store the files 
     $input = $form.find('input[type="file"]'); // find the input field to attach dropped files 
     console.log($input.attr('id')); // check right input item selected 
     $.each(droppedFiles, function(i, file) { // loop round all files attached 
      files.append($input.attr('name'), file); 
     }); 
     $form.trigger('submit'); // auto submit form just for testing 
     }) 

一切似乎工作,但拖放文件不附加並提交。

+0

想通了這一點,我在一個單獨的函數中創建一個新的FormData對象,覆蓋了這個所有的工作。感謝Mike的語法拼寫糾正已連續四天完成,並且只是在咖啡上運行完畢,因此打字時有點sl py –

回答

0

想通了,我在一個獨立的函數中創建了一個新的FormData對象,這個函數覆蓋了這個工作。感謝邁克語法拼寫更正已連續四天得到這個項目完成,並在咖啡上運行,所以打字有點馬虎