2011-02-24 70 views
2

我正在使用jQuery File upload plugin。在單個頁面上有多個文件上傳器的實例。

看看這裏的一個例子在jsFiddle

$(function() { 
    $('.file_upload').fileUploadUI({ 
     uploadTable: $('#files'), 
     downloadTable: $('#files'), 
     buildUploadRow: function (files, index) { 

      // HOW TO DETERMINE WHICH FILE_UPLOADER Was Clicked? 
      // Need a reference point so I can find the right, #files1 or #files2 

      return $('<tr><td>' + files[index].name + '<\/td>' + 
        '<td class="file_upload_progress"><div><\/div><\/td>' + 
        '<td class="file_upload_cancel">' + 
        '<button class="ui-state-default ui-corner-all" title="Cancel">' + 
        '<span class="ui-icon ui-icon-cancel">Cancel<\/span>' + 
        '<\/button><\/td><\/tr>'); 
     }, 
     buildDownloadRow: function (file) { 
      return $('<tr><td>' + file.name + '<\/td><\/tr>'); 
     } 
    }); 
}); 

我遇到的問題是,當用戶單擊上傳文件,我不知道他們點擊其中之一。我需要知道他們點擊了哪一個,因爲我想要插件的buildUploadRow等。知道在哪裏構建行。我嘗試使用$(this),但沒有得到選擇器,表單元素,這是我所需要的。

回答

2

通過利用jQuery's each method就可以得到所需的參考上傳表單,它允許你指定相關uploadTable/downloadTable:

$('.file_upload').each(function() { 
    var uploadTable = downloadTable = $(this).next('table'); 
    $(this).fileUploadUI({ 
     uploadTable: uploadTable, 
     downloadTable: downloadTable, 
     buildUploadRow: function (files, index) { 
      return $('<tr><td>' + files[index].name + '<\/td>' + 
        '<td class="file_upload_progress"><div><\/div><\/td>' + 
        '<td class="file_upload_cancel">' + 
        '<button class="ui-state-default ui-corner-all" title="Cancel">' + 
        '<span class="ui-icon ui-icon-cancel">Cancel<\/span>' + 
        '<\/button><\/td><\/tr>'); 
     }, 
     buildDownloadRow: function (file) { 
      return $('<tr><td>' + file.name + '<\/td><\/tr>'); 
     } 
    }); 
}); 

有關jQuery的文件上傳插件其他疑問,歡迎免費在項目網站上使用issue tracker

相關問題