简体   繁体   English

无法将输入事件从动态加载的表单绑定到jQuery多文件

[英]Cannot bind input event to jQuery multifile from dynamically loaded form

I am getting strange behaviour when trying to upload files, using jQuery multi-file plugin, from a dynamically loaded form. 尝试使用jQuery多文件插件从动态加载的表单上载文件时,出现奇怪的行为。

I'm using Firefox 9.0.1/Mac 我正在使用Firefox 9.0.1 / Mac

This is how I am trying to bind to the change event: I have tried blur as well (and click and...) 这就是我试图绑定更改事件的方式:我也尝试了模糊(然后单击并...)

$('#newticketform').live('change',function (e){ //newticket form is the form in which my input type=file is contained       
         $('#my_file_element').MultiFile(); //my_file_element is the input type=file element
});

Should the binding be to the form or input field? 绑定应该是表单还是输入字段? I did try both without any difference in behaviour. 我确实尝试了两者,但没有任何差异。

When using .on instead of .live the function above is not triggered at all. 当使用.on而不是.live时,上述功能根本不会触发。

I've managed to upload files before loading the form as dynamic content. 在将表单作为动态内容加载之前,我设法上传了文件。 When loading the form into my main page I have to, of course, bind the event in some way. 当将表单加载到我的主页时,我当然必须以某种方式绑定事件。

This is the what happens: 这是发生了什么:

  • 1st time: Nothing happens (but he function above is triggered, confirmed using alert). 第一次:什么也没有发生(但上面的功能已触发,已通过警报确认)。 Ie no file is attached to the lista of files to be uploaded. 即没有文件附加到要上传的文件列表中。
  • 2nd time: The file is added to the list. 第二次:将文件添加到列表中。

It seems like the binding is not realised before the first time I try to add a file and the second time it's working. 似乎在我第一次尝试添加文件之前和第二次工作之前都未实现绑定。

Just in case I'm including the html as well: 以防万一我也包括html:

<form method="post" enctype="multipart/form-data" id="newticketform">
        <input type="hidden" value="2000000" name="MAX_FILE_SIZE">
        <label for="title">Rubrik</label> <input type="text" name="title" id="title"><br><br>
                 <label for="description">Beskrivning</label> <textarea name="description" id="description" cols="50" rows="15"></textarea><br> 

                 <input type="file" maxlength="5" name="file[]" id="my_file_element" class="multi">
                 <div id="files_list"></div>
                 <input type="submit" value="Upload" name="upload"> 
 </form>

Tested this after feedback from Jasper below: 在下面的Jasper反馈之后进行了测试:

$("#newticketmenu").live('click',function(event){
          $("#adminarea").load("http://" + hostname + "/" + compdir + "/modules/core/newticket/newticket.php", function(){
                $('#newticketform').on('change', '#my_file_element', function (){     
                    $(this).MultiFile();
                })
                addNewTicketValidation();                                          
          });
      });

Still, exactly the same behaviour. 仍然,行为完全相同。

All JavaScript files are loaded together with the main page. 所有JavaScript文件都与主页一起加载。

What am I doing wrong? 我究竟做错了什么? Is my way of binding incorrect? 我的绑定方式不正确吗?

Thanks! 谢谢!

Well the plugin MultiFile needs to be called before the user interacts with the file input. 那么,在用户与文件输入进行交互之前,需要调用插件MultiFile You should call the MultiFile plugin on the element as soon as it is added to the DOM. 您应该在元素上将MultiFile插件添加到DOM后立即对其进行调用。

I'm not sure how you are dynamically adding the form to the page but here's a stab at it: 我不确定您是如何将表单动态添加到页面中的,但是这里有一个刺痕:

$.ajax({
    url     : '<url>',
    success : function (serverResponse) {
        $('#my-form-container').html(serverResponse).find('#my_file_element').MultiFile();
    }
});

On a side-note, your code seems to be binding to a form for the change event, which is supposed to be bound to input elements within a form. 附带说明一下,您的代码似乎绑定到了change事件的表单,该事件应该绑定到表单中的输入元素。 You could try this: 您可以尝试以下方法:

$('#my-form-container').delegate('#my_file_element', 'change',function (){     
    $(this).MultiFile();
});

Notice I used .delegate() instead of .live() as the latter has been depreciated as of jQuery 1.7. 注意,我使用.delegate()代替.live()因为后者已从jQuery 1.7开始贬值。 If you are using jQuery 1.7+ then you can use .on() in a similar fashion to delegate event handling: 如果您使用的是jQuery 1.7+,则可以类似的方式使用.on()来委托事件处理:

$('#my-form-container').on('change', '#my_file_element', function (){     
    $(this).MultiFile();
});

Notice that the order of the parameters for .delegate() and .on() are different. 请注意, .delegate().on()的参数顺序不同。

If you are setting the event binding inside the callback function (which you are according to your example) for you AJAX request, then you don't need to use event delegation, you can just run the plugin on the element directly after you add it to the DOM: 如果要为AJAX请求设置回调函数(根据示例)中的事件绑定,则不需要使用事件委托,只需在添加元素后直接在元素上运行插件到DOM:

    $("#adminarea").load("http://" + hostname + "/" + compdir + "/modules/core/newticket
              /newticket.php", function(){ 
              $('#my_file_element').MultiFile(); 
              addNewTicketValidation(); 
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM