the*_*ski 12 javascript jquery html5 filereader
我有一个文件输入:
<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>
Run Code Online (Sandbox Code Playgroud)
功能如下:
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;
};
};
Run Code Online (Sandbox Code Playgroud)
一切都很完美.现在我有一个更改按钮.我希望如果有人点击它,那么之前上传的文件细节就会消失.功能如下所示:
$('#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.
});
Run Code Online (Sandbox Code Playgroud)
你可以帮忙吗?
Aer*_*ous 17
如果您希望将数据保存在其他表单字段中,则可以使用
HTML
<input type='file' id='yourid' />
Run Code Online (Sandbox Code Playgroud)
jQuery的
$('#yourid').val('');
Run Code Online (Sandbox Code Playgroud)
这将从输入标签中清除您上传的文件
sud*_*dee 10
如果将文件输入放在<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>
Run Code Online (Sandbox Code Playgroud)
JS:
$('#changeImage').click(function(){
$('#uploadPreview').addClass('hide');
$('#customImage').removeClass('hide');
// Reset the form
$("#fileform")[0].reset();
});
Run Code Online (Sandbox Code Playgroud)
没有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();
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21311 次 |
| 最近记录: |