在事件中访问$(this)(jquery文件上传) [英] access $(this) in event (jquery file upload)

查看:119
本文介绍了在事件中访问$(this)(jquery文件上传)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在jquery文件上传中适当地,您可以访问上下文,例如在我未使用的ui版本中,将$(this)data.context结合使用(我使用的是基本版本).

Apprently in jquery fileupload you can access the context e.g. $(this) with data.context in the ui version, which I am not using (im using the basic version).

我可以在除error之外的所有事件中正确访问$(this),我正在做一点黑客工作,以使用'global'变量使其正常工作.但是我觉得似乎有更好的首选方法了?

I can access $(this) properly in all the events except for error, I am doing a bit of a hack to get it to work by using 'global' variables. But I felt as though there might be a better more preferred way?

$('.fileupload').fileupload({
            dataType: 'json',
            url: 'framework/AJAX/image_handler.php?cmd=upload',
            //maxChunkSize: 1000000,
            start: function (e, data) {
                obj = $(this); // bit of a hack
                pb = obj.parent().find('.fileinput-new');
                $('#move-to').attr('disabled', 'disabled');
            },
            progressall: function (e, data) {
                var progress = parseInt(data.loaded / data.total * 100, 10);
                $(this).attr('disabled', 'disabled');
                pb.html('Uploading: ' + progress + '%');
                if (progress >= 100) {
                    pb.html('Processing...');
                }
            },
            done: function (e, data) {
                $(this).removeAttr('disabled');
                if (countProcessing() === 0) {
                    $('#move-to').removeAttr('disabled');
                }
                pb.html('Select image');
                var error = data.jqXHR.responseJSON.files[0].error;
                if (error) {
                    asengine.displayErrorMessageBody(error);
                } else {
                    var img_num = $(this).attr('data-id');
                    var format = '<?php echo $images_core->formatImagePath($id, false) . $images_core->thumb_prepend . $id . '_'; ?>' + img_num + '.<?php echo $images_core->default_ext; ?>?' + uniqueId();
                    $(this).closest('.fileinput').find('img').attr('src', format);
                    $('#remove' + img_num).load(location.href + ' #remove' + img_num);
                }
            },
            error: function (e, data) {
                // need to access this here...
                pb.next().removeAttr('disabled');
                pb.html('Try again');
                if (countProcessing() === 0) {
                    $('#move-to').removeAttr('disabled');
                }
                asengine.displayErrorMessageBody('An unknown error occured.');
            }
        }).bind('fileuploadsubmit', function (e, data) {
            data.formData = {
                'id': '<?php echo $id; ?>',
                'img_number': $(this).attr('data-id')
            };
        });

推荐答案

在文档中,我发现了这一点: https://github.com/blueimp/jQuery-File-Upload/wiki/Multiple-File-Upload-Widgets-on-the-同一页

Buried in documentation I found this: https://github.com/blueimp/jQuery-File-Upload/wiki/Multiple-File-Upload-Widgets-on-the-same-page

这导致了我的解决方案:

Which led to my solution:

$('.fileupload').each(function () {
    var obj = $(this);
    var pb = obj.parent().find('.fileinput-new');
    $(this).fileupload({
        dataType: 'json',
        url: 'framework/AJAX/image_handler.php?cmd=upload',
        autoUpload: true,
        start: function (e, data) {
            $('#move-to').attr('disabled', 'disabled');
        },
        progressall: function (e, data) {
            var progress = parseInt(data.loaded / data.total * 100, 10);
            $(this).attr('disabled', 'disabled');
            pb.html('Uploading: ' + progress + '%');
            if (progress >= 100) {
                pb.html('Processing...');
            }
        },
        done: function (e, data) {
            $(this).removeAttr('disabled');
            if (countProcessing() === 0) {
                $('#move-to').removeAttr('disabled');
            }
            pb.html('Select image');
            var error = data.jqXHR.responseJSON.files[0].error;
            if (error) {
                asengine.displayErrorMessageBody(error);
            } else {
                var img_num = $(this).attr('data-id');
                var format = '<?php echo $images_core->formatImagePath($id, false) . $images_core->thumb_prepend . $id . '_'; ?>' + img_num + '.<?php echo $images_core->default_ext; ?>?' + uniqueId();
                $(this).closest('.fileinput').find('img').attr('src', format);
                $('#remove' + img_num).load(location.href + ' #remove' + img_num);
            }
        },
        error: function (e, data) {
            pb.next().removeAttr('disabled');
            pb.html('Try again');
            if (countProcessing() === 0) {
                $('#move-to').removeAttr('disabled');
            }
            asengine.displayErrorMessageBody('An unknown error occured.');
        }
    }).bind('fileuploadsubmit', function (e, data) {
        data.formData = {
            'id': '<?php echo $id; ?>',
            'img_number': $(this).attr('data-id')
        };
    });
});

这篇关于在事件中访问$(this)(jquery文件上传)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆