2011-02-24 67 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

相关问题