问题描述
当用户选择文件时,我希望显示另一个文件字段。它适用于第一个文件,但如果我在第二个字段中选择一个文件,则不会再次调用它。为什么不呢?
When a user selects a file, I want another file field to appear. It works for the first file, but if i choose a file in the second field, it doesn't get called again. Why not?
jquery script
jquery script
$(document).ready(function() {
var i = 1;
$('input:file[name$=\'uploadedFile'+(i-1)+'\']').change(function() {
var file = $(this).val();
if(file !== null && file !== "") {
$(this).after("<input type=\"file\" name=\"uploadededFile"+i+"\" />");
i++;
}
});
});
html表格
<form action="PHP/file.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="500000" />
<input type="file" name="uploadedFile0" />
<input type="submit" value="SUBMIT" />
</form>
推荐答案
当你写 $( ...)。change(function)
,您正在将处理程序添加到jQuery对象中当前的元素中。当您添加新的< input>
时,它没有任何事件处理程序。
When you write $(...).change(function)
, you are adding the handler to the elements that are currently in the jQuery object. When you add a new <input>
, it doesn;t have any event handlers.
您需要致电,这将处理所有匹配元素的事件,无论它们何时被创建。
例如:
You need to call .live
, which will handle the event for all matching elements, no matter when they were created.
For example:
$('input:file:last').live('change', function() { ... });
请注意,选择器仅评估一次,因此它不会在<$ c中获取更改$ c>我。
相反,你应该使用:last
。
Note that the selector is only evaluated once, so it won't pick up changes in i
.
Instead, you should use :last
.
这篇关于jquery form type =" file"如何判断何时选择了文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!