无法将输入事件绑定到动态加载表单中的 jQuery 多文件
当尝试使用 jQuery 多文件插件从动态加载的表单上传文件时,我遇到了奇怪的行为。
我正在使用 Firefox 9.0.1/Mac
这就是我尝试绑定到更改事件的方式:我也尝试过模糊(然后单击并...)
$('#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
});
应该绑定到表单还是输入字段?我确实尝试了两者,行为上没有任何差异。
当使用 .on 而不是 .live 时,上面的函数根本不会被触发。
我已成功在将表单加载为动态内容之前上传文件。当将表单加载到我的主页时,我当然必须以某种方式绑定事件。
这是发生的情况:
- 第一次:什么也没发生(但是上面的函数被触发了, 使用警报确认)。即没有文件附加到列表中 要上传的文件。
- 第二次:文件被添加到列表中。
在我第一次尝试添加文件和第二次工作之前,绑定似乎没有实现。
以防万一我也包含了 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>
在下面 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();
});
});
仍然是完全相同的行为。
所有 JavaScript 文件都与主页一起加载。
我做错了什么?是不是我的绑定方式不对?
谢谢!
I am getting strange behaviour when trying to upload files, using jQuery multi-file plugin, from a dynamically loaded form.
I'm using 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.
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). I.e. 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:
<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:
$("#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.
What am I doing wrong? Is my way of binding incorrect?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那么,在用户与文件输入交互之前需要调用插件
MultiFile
。您应该在元素添加到 DOM 后立即调用该元素的MultiFile
插件。我不确定您如何动态地将表单添加到页面,但这里有一个说明:
顺便说一句,您的代码似乎绑定到
change
事件的表单,该表单应该绑定到表单内的输入元素。您可以尝试以下操作:注意,我使用了
.delegate()
而不是.live()
,因为后者从 jQuery 1.7 开始已被弃用。如果您使用的是 jQuery 1.7+,那么您可以以类似的方式使用.on()
来委托事件处理:请注意
.delegate()
的参数顺序和.on()
是不同的。.on()
的文档:http://api.jquery.com/on.delegate()
的文档: http://api.jquery.com/delegate如果您正在设置事件绑定回调函数(根据您的示例)用于您的 AJAX 请求,那么您不需要使用事件委托,您可以在将其添加到 DOM 后直接在元素上运行插件:
Well the plugin
MultiFile
needs to be called before the user interacts with the file input. You should call theMultiFile
plugin on the element as soon as it is added to the DOM.I'm not sure how you are dynamically adding the form to the page but here's a stab at it:
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. You could try this:Notice I used
.delegate()
instead of.live()
as the latter has been depreciated as of jQuery 1.7. If you are using jQuery 1.7+ then you can use.on()
in a similar fashion to delegate event handling:Notice that the order of the parameters for
.delegate()
and.on()
are different..on()
: http://api.jquery.com/on.delegate()
: http://api.jquery.com/delegateIf 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: