Javascript FormData到动态创建的元素

Javascript FormData到动态创建的元素,javascript,jquery,Javascript,Jquery,我不明白为什么不能从动态创建的元素通过xhr发送文件。这是我的代码: $('#thread_file').prepend('<form action="/nti/'+$time+'" enctype="multipart/form-data" method="post"><div class="picture" style="text-align:right;height:30px;line-height:15px;">change file<

我不明白为什么不能从动态创建的元素通过xhr发送文件。这是我的代码:

            $('#thread_file').prepend('<form action="/nti/'+$time+'" enctype="multipart/form-data" method="post"><div class="picture" style="text-align:right;height:30px;line-height:15px;">change file</div><input type="hidden" name="thread" value="a" /><input class="file" data-thread="a" type="file" name="file" accept="image/*" style="display:none!important;" /><progress value="0" min="0" max="100">0%</progress></form>');
                $('form').on('click','.picture',function(){$(this).parent().find('.file').click();});
                $('form').on('change','.file',function(){
                    var $form = $(this).parent();
                    var data = new FormData($form);
                    var xhr = new XMLHttpRequest();
                    var $progress_bar = $form.find('progress');
                    xhr.open('post',$form.action);
                    xhr.onload = function(e)
                    {
                        $form.parent().find('.thread_file').html(e.currentTarget.responseText);
                    };
                    xhr.upload.onprogress = function(e) 
                    {
                        $progress_bar.val(e.loaded/e.total*100);
                    };
                    xhr.send(data);
                    return false;
                });
$(“#线程文件”).prepend('change file0%”);
$('form')。在('click','.picture',function(){$(this.parent().find('.file')。click();});
$('form')。在('change','.file',function()上{
var$form=$(this.parent();
var数据=新的FormData($form);
var xhr=new XMLHttpRequest();
var$progress\u bar=$form.find('progress');
xhr.open('post',$form.action);
xhr.onload=函数(e)
{
$form.parent().find('.thread_file').html(e.currentTarget.responseText);
};
xhr.upload.onprogress=函数(e)
{
$progress_bar.val(e.loaded/e.total*100);
};
发送(数据);
返回false;
});

您需要将表单dom元素传递给FormData,而不是jQuery包装器对象

var data = new FormData($form[0]);
读取action参数也一样,因为$form是一个jQuery对象,用于读取action属性的值或使用
$form[0]。action

xhr.open('post', $form.attr('action'));

演示:

我不明白为什么你会像这样在我们身上转储代码,并期望我们在没有任何事情进行的情况下调试它。