我有一个文件输入:
<img id="uploadPreview">
<div id="changeImage">Change</div>
<div id="customImage">
<input type="file" id="myfile" multiple style="display:none" onchange="PreviewImage();" />
<div class='upload blank' id="add-image"></div>
</div>
功能如下:
var oFReader = new FileReader();
oFReader.readAsDataURL(document.getElementById("myfile").files[0]);
oFReader.onload = function (oFREvent) {
document.getElementById("uploadPreview").src = oFREvent.target.result;
};
function PreviewImage() {
var oFReader = new FileReader();
oFReader.readAsDataURL(document.getElementById("myfile").files[0]);
$("#uploadPreview").removeClass('hide'); //for manipulating something in the dom
$('#changeImage').removeClass('hide'); //for manipulating something in the dom
$("#customImage").addClass('hide'); //these are for manipulating something in the dom
oFReader.onload = function (oFREvent) {
document.getElementById("uploadPreview").src = oFREvent.target.result;
};
};
一切都很完美。现在我有一个更改按钮。我希望如果有人点击它,那么之前上传的文件细节就会消失。功能如下所示:
$('#changeImage').click(function(){
$('#uploadPreview').addClass('hide');
$('#customImage').removeClass('hide');
//here I want to remove/clear the details about the already previous uploaded file in the 'file-input'. So the same image can be shown if someone clicks it for once again.
});
你可以帮忙吗?
答案 0 :(得分:15)
如果您希望将数据保存在其他表单字段中,可以使用
HTML
<input type='file' id='yourid' />
的jQuery
$('#yourid').val('');
这将从输入标记
中清除您上传的文件答案 1 :(得分:9)
如果您将文件输入放在<form>
标记内,则可以使用内置的.reset()
方法。
<强> HTML:强>
<img id="uploadPreview">
<div id="changeImage">Change</div>
<div id="customImage">
<form id="fileform">
<input type="file" id="myfile" multiple style="display:none" onchange="PreviewImage();" />
</form>
<div class='upload blank' id="add-image"></div>
</div>
<强> JS:强>
$('#changeImage').click(function(){
$('#uploadPreview').addClass('hide');
$('#customImage').removeClass('hide');
// Reset the form
$("#fileform")[0].reset();
});
没有jQuery的JS:
var resetButton = document.getElementById('resetButton');
var fileInput = document.getElementById('fileInput');
var form = document.getElementById('form');
resetButton.addEventListener('click', function () {
// resetting the file input only
fileInput.value = null;
// alternatively: resetting the entire form (works in older browsers too)
form.reset();
});
答案 2 :(得分:0)
在React
中,我在基于chrome的浏览器上输入文件时遇到此问题。我使用value=""
和autoComplete={"new-password"}
<input
value=""
type="file"
autoComplete={"new-password"}
onChange={e => pickHandler(e)}
/>