django-ajax-uploader:上传成功时获取点击的元素

django-ajax-uploader: get the clicked element on upload success

我正在使用 django-ajax-uploader 上传带有 Ajax 的文件: https://github.com/skoczen/django-ajax-uploader.

我想在上传成功后获取点击的元素。 这是代码:

$('.file-uploader').each(function()
{
    input_element=$(this);

    var uploader = new qq.FileUploader(
    {
        action: "{% url 'campaigns:my_ajax_upload' %}" ,
        element: input_element[0],
        multiple: false,
        onComplete: function(id, fileName, responseJSON) 
        {
            ...
        },
        onAllComplete: function(uploads) 
        {
            ...
            //how to get the element that triggered the upload?
        },
    });
});

这里的问题是同一个页面有很多file-uploader个元素,所以如果我用input_element获取触发上传的点击按钮,我只会获取已上次加载!

提前致谢。

刚刚找到答案,您可以 bind onAllComplete 这样您就可以在 callback 方法中使用 this

onAllComplete: function(uploads) 
{
       window.console&&console.log("All complete!");
       //this is accessible and binded to the right object
       $(this).closest(".show_hide") ... ;
}.bind(this),

我在另一个 SO post 中找到了答案: