微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

jquery – Chrome文件上传错误:更改事件不会同一个文件执行两次

我正在使用这个html5文件上传插件,但它在Google Chrome上有一个错误,我无法理解并修复它。它在Firefox上工作正常。

jsfiddle link

问题是您无法从桌面上传相同的文件两次。

当您第一次点击时,选择并点击确定从桌面上传文件,它应该提醒一个消息,例如’.button-1′ – 取决于你点击哪个上传按钮。

那么如果您尝试再次上传相同的文件,这行代码将不会被执行,

$(".upload-file",object_parent).change(function() {

             ...
             ...

             alert($cm.selector);

});

任何想法这个插件出了什么问题?

(function($){

    // Attach this new method to jQuery
    $.fn.extend({ 

        // This is where you write your plugin's name
        upload_file_html5: function(options) {

            // Set the default values,use comma to separate the settings,example:
            var defaults = {
                objectSuperparent:    '.media'
            }

            var options =  $.extend(defaults,options);
            var o = options;

            var $cm = this.click(function(e){

                // <a> button is the object in this case.
                var object = $(this);

                // Get other info from the element belong to this object group.
                var object_href = object.attr('href');
                var object_parent = object.parent();
                alert($cm.selector);

                // Trigger the click event on the element.
                // Due to security policies triggering click on the input type=file is not allowed/supported in some browsers and Opera is one of them.
                //$('input[type=file]').trigger('click'); // or:
                $(".upload-file",object_parent).click();

                return false;

            });

            // Trigger ajax post when ever the file is changed by the user.
            var $cm_2 = $(".upload-file").change(function(){

                // <input> is the object in this case.
                var object = $(this);

                var object_form = object.parent();
                var object_superparent = object.parents(o.objectSuperparent);
                var path_config = $($cm.selector,object_superparent).attr('href');
                var path_post = object_form.attr('action');

                alert($cm.selector);
                //alert(path_config);

                ....
                ....

            });

        }
    });

})(jQuery);

Chrome可以正常工作,但最近刚刚失败,可能Chrome已将最新版本更新到我的机器,此更新会导致错误

解决方法

是。我的Chrome对Firefox有不同的行为,但我认为Chrome是正确的。

根据W3C’s document

onchange event occurs when a control loses the input focus and its value has been modified since gaining focus

如果您尝试上传相同的文件文件输入的值不会更改。尝试打印出来:

$('.button-2').click(function(){
    console.log($(".list .upload-file").val())
    return false;
});

原文地址:https://www.jb51.cc/jquery/181782.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐