admin管理员组文章数量:1390803
I am making an upload form and I want users to add more input file fields to the form if they require using jquery if they want to upload more than one file. This is the code I have so far below that isn't working. You can also see this on js fiddle: /
Thanks in advance.
$(function () {
$('#add-more-files').click(function() {
var cloned = $(this).prev().clone();
cloned.val(null);
$(cloned).insertBefore($(this));
});
});
<ul>
<form enctype="multipart/form-data" action="" method="post">
<li>Choose a file to upload:</li>
<li><input name="uploadedfile" type="file" size="40" /></li>
<li><a href="" id="add-more-files">Add file upload box</a></li>
<li><input type="submit" value="Upload File" /></li>
</form>
</ul>
I am making an upload form and I want users to add more input file fields to the form if they require using jquery if they want to upload more than one file. This is the code I have so far below that isn't working. You can also see this on js fiddle: http://jsfiddle/benpaton/JUJxn/
Thanks in advance.
$(function () {
$('#add-more-files').click(function() {
var cloned = $(this).prev().clone();
cloned.val(null);
$(cloned).insertBefore($(this));
});
});
<ul>
<form enctype="multipart/form-data" action="" method="post">
<li>Choose a file to upload:</li>
<li><input name="uploadedfile" type="file" size="40" /></li>
<li><a href="" id="add-more-files">Add file upload box</a></li>
<li><input type="submit" value="Upload File" /></li>
</form>
</ul>
Share
Improve this question
asked May 31, 2012 at 23:38
Ben PatonBen Paton
1,4429 gold badges37 silver badges60 bronze badges
2 Answers
Reset to default 3You're not selecting the <li>
element.
Also, your anchor needs a hash to prevent it from trying to resolve a url
jsFiddle
HTML
<ul>
<form enctype="multipart/form-data" action="" method="post">
<li>Choose a file to upload:</li>
<li><input name="uploadedfile" type="file" size="40" /></li>
<li><a href="#" id="add-more-files">Add file upload box</a></li>
<li><input type="submit" value="Upload File" /></li>
</form>
</ul>
JS
$(function () {
//clone file upload box
$('#add-more-files').click(function() {
var cloned = $(this).parent().prev().clone();
cloned.val(null);
$(cloned).insertBefore($(this).parent());
});
});
It looks like you're trying to select something that doesn't exist. $('#add-more-files').prev()
will be an empty jQuery object. $.prev()
selects the previous sibling, of which there are none given the selector you've supplied.
I believe you want something like $(this).parent().prev().clone()
本文标签: javascriptClone file upload input filed in form with jqueryStack Overflow
版权声明:本文标题:javascript - Clone file upload input filed in form with jquery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744748679a2623041.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论