追加()中的jquery一点儿也不附加文件
我已经定义文件类型和它下面的隐藏形式的输入第二时间,追加()中的jquery一点儿也不附加文件
<input type="file" name="attachment0" id="attachment0" custom-on-change="uploadfile" ng-show="attachFile" multiple/>
<form class="hidden" id="myForm" method="post" enctype="multipart/form-data" action="SupportRequest">
</form>
“定制电平变化”是角指令调用函数uploadfile()选择文件后。我的JavaScript看起来如下所示
var form = $('#myForm');
var n=0;
$scope.attachFile = true;
$scope.uploadfile = function() {
if(n == 0){
var filein = $('#attachment0');
form.append(filein);
$('#attachment0').test();
}
else
{
if(n==1){
var temp = "attachment0" + '_' + n;
var file_in = $('#'+temp);
form.append(file_in);
$('#'+temp).test();}
}
};
$.fn.test = function() {
return this.each(function(){
n++;
$(this).attr('id', this.id + '_' + n);
$(this).attr('name', this.name + '_' + n);
});
};
我试图将文件两次追加到形式myForm。当我第二次选择文件时,即使在附加文件后,我也发现先前附加的id =“attachment0”的输入不存在于myForm中,id =“attachment0_1”的输入存在于表单中。 这里我试图多次上传多个文件,这就是为什么我动态改变了test()中输入的id。 我希望在myForm中输入id id attachment0和attachment0_1。 有谁知道如何解决这个问题?并解释这种行为。
看起来像test
功能要更换ID,
$(this).attr('id', this.id + '_' + n);
会不会更好地使用ng-repeat
?事情是这样的:
控制器:
$scope.files = [...] // list of files
$scope.uploadfile = function() {
// So stuff to get the file
$scope.files.push(filein);
};
HTML:。
<!-- Inside the form -->
<div ng-repeat="file in files">{{file}}</div>
我不试图在这里打印文件,我试图将输入类型的文件附加到myForm中。 –
更改您的代码从form.append(FILEIN)至form.append($( '#attachment0')克隆())。
然后它工作。您应该使用.clone()方法来复制元素。
.append()只是移动从一个地方的元素到另一个但对于第一次复制使用.clone()创建元素的副本,然后将其添加到您的窗体
这工作完美。 –
但该文件无法复制,请检查此问题http://stackoverflow.com/questions/14772947/how-to-clone-a-file-input-to-another-file-input-which-is-in-hidden- IFRAME –
是否有浏览器的控制台任何错误? – Luke
我可以问你为什么在使用AngularJS时使用jQuery来更新UI吗? –
大家好,我已经更新了描述。我基本上试图多次上传多个文件,这就是为什么我动态更改我的输入的ID。 –