Событие изменения запускается дважды, а не один раз - PullRequest
0 голосов
/ 21 января 2012

Я работаю над этим плагином для загрузки файлов html5, но в нем есть ошибка, которую я не могу понять и исправить.

jsfiddle link

Идея этого плагина состоит в том, чтобы прикреплять группу функций к целевой кнопке, только когда она нажата.

Но когда я нажимаю на событие 'button-1' , событие нажатия 'button-2' срабатывает одновременно с происходит. Я думаю, что событие изменения должно было инициироваться дважды.

Вы можете попробовать нажать кнопку загрузки и посмотреть, что я имею в виду.

Есть идеи, что не так в этом плагине?

(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);

1 Ответ

0 голосов
/ 21 января 2012

Проверьте мой вариант вашего кода: http://jsfiddle.net/es8Vh/3/ Базовая структура плагина jQuery:

$.fn.pluginName = function() {
    return this.each(function() { // <-- this is important
        // code here
    });
};
...