Javascript 在以前的更改发生时附加新的文件上载按钮
我有一个函数,当用户添加文件时,使用jQuery添加一个额外的文件上传按钮。我的问题是,我似乎无法让它以正确的格式添加或每次添加。我当前的函数只是尝试直接添加字符串:Javascript 在以前的更改发生时附加新的文件上载按钮,javascript,jquery,Javascript,Jquery,我有一个函数,当用户添加文件时,使用jQuery添加一个额外的文件上传按钮。我的问题是,我似乎无法让它以正确的格式添加或每次添加。我当前的函数只是尝试直接添加字符串: jQuery(document).ready(function($) { $(function() { $("input:file").change(function(){ $("input:file").after("</td></tr><tr>
jQuery(document).ready(function($) {
$(function() {
$("input:file").change(function(){
$("input:file").after("</td></tr><tr><td class=\"field_name span4\"><strong></strong></td><td class=\"field_option\"><input type=\"file\" name=\"pictures\">");
});
});
});
但是,这根本不会追加(理论上可能会追加每个元素),并且我不能在input.get(count)上使用.after()
简单地说,我希望改进这一点,使其能够附加一个新的文件上传按钮,并且不会被错误格式化。如果可能,我宁愿使用方法2,但目前我希望它能够正常工作。您需要将处理程序重新添加到每个新的文件输入中,因此最好在函数中隔离该处理程序:
var addfile = function() {
var newRow = $("<tr><td class=\"field_name span4\"><strong></strong></td>" +
"<td class=\"field_option\"><input type=\"file\" name=\"pictures\">").
insertAfter($(this).closest('tr'));
newRow.find('input:file').change(addfile);
};
$("input:file").change( addfile );
var addfile=function(){
var newRow=$(“”+
"").
insertAfter($(this.closest('tr'));
newRow.find('input:file').change(addfile);
};
$(“输入:文件”).change(addfile);
我刚刚试用了你的现场演示,它正在插入一些东西。但它当然只发生一次,因为事件处理程序仅绑定到第一个对象
我会尝试使用以下类似的更改功能:
$('input:file').change(function() {
$(this).closest('tr').after($(this).closest('tr').clone());
});
但是您的更改处理程序也必须重新生成。如果不附加开始标记,就不能附加结束标记。当处理一个关闭的文档(在本例中是这样)时,您不再处理打开和关闭标记,而是处理有父节点和子节点的元素节点。@KevinB我估计是这样,但目前它至少产生了一些东西。第二种(首选)方法根本没有任何效果。我建议克隆当前tr并将其附加到当前tr之后。
$('input:file').change(function() {
$(this).closest('tr').after($(this).closest('tr').clone());
});