【发布时间】:2015-11-17 12:40:02
【问题描述】:
================编辑================================= =====================
您好!非常感谢所有回答并试图帮助我的人。我能够按照@Kaiido 的建议使用 iframe 来获得我需要的东西,因为我以前的代码在 ie8 中不起作用。也感谢这一点:JQuery file posting using iframe 再次。非常感谢!
============================================== ============================
我有一个上传表格。我可以在提交时上传文件。问题是一旦用户选择要上传的文件,我就会尝试添加/附加删除按钮。
我需要它看起来像这样:
目标:
1.当用户点击删除按钮时,文件列表应该回到空。
2. 更改文件的行为应该类似于单击“删除”按钮。
3. 它需要在 IE 8 中工作。它很烂。
输入表格:
Attachment: <input type="file" name="upload" id="upload">
<a href="#" id="btnSubmit">Submit</a>
JS:
var files;
$('input[type=file]').on('change', prepareUpload);
function prepareUpload(event)
{
files = event.target.files; // I'm not sure but the problem might be on this part. Maybe I could get the opposite of this or negate this?
}
function uploadFiles(event)
{
event.stopPropagation();
event.preventDefault();
var data = new FormData();
$.each(files, function(key, value)
{
data.append(key, value);
});
$.ajax({
url: 'execs/upload.php?files',
type: 'POST',
data: data,
cache: false,
dataType: 'json',
processData: false,
contentType: false,
success: function(data)
{
console.log('Uploaded');
},
error: function()
{
console.log('Failed');
}
})
}
$("#btnSubmit").click(function(e)
{
uploadFiles(event);
})
非常感谢您的帮助!
【问题讨论】:
-
嗨@DJDavid98!我确实在那里使用了该解决方案,但是,它只清除了该字段。文件列表不为空。
-
@Mary,
input.value=''解决方案应该可以工作,只需避免将文件列表设为全局变量即可。 prepareUpload 函数没有用,你只需要在上传时检查输入的files。或者,您的删除函数应该只删除全局file变量 -
嗨@Kaiido!我尝试为值分配一个空值。我检查了 files[0] 的值,它仍然不是空的。谢谢!
-
来自
<input>的那个?你在哪个浏览器上试过? -
是的。我需要让它在ie8中工作。谢谢!
标签: javascript jquery