使用JavaScript上传文件时,我无法显示单张图片。请帮帮我
形式:
<td><div id="filediv"><input name="file_img[]" type="file" id="file" /><br></div>
<div id="filediv"><input name="file_img[]" type="file" id="file" /><br></div>
JavaScript的:
每次点击JavaScript图片(您可以上传多个文件
var abc = 5; // Declaring and defining global increment variable.
$(document).ready(function() {
// To add new input file field dynamically, on click of "Add More Files" button below function will be executed.
// Following function will executes on change event of file input to select different file.
$('body').on('change', '#file', function() {
if (this.files && this.files[0]) {
abc += 4; // Incrementing global variable by 1.
var z = abc - 2;
var x = $(this).parent().find('#previewimg' + z).remove();
$(this).before("<div id='abcd" + abc + "' class='abcd'><img id='previewimg" + abc + "' src=''/></div>");
var reader = new FileReader();
reader.onload = imageIsLoaded;
reader.readAsDataURL(this.files[0]);
$("#abcd" + abc), {
id: 'img',
src: 'x.png',
alt: 'delete'
}.click(function() {
$(this).parent().remove();
});
}
});
// To Preview Image
function imageIsLoaded(e) {
$('#previewimg' + abc).attr('src', e.target.result);
};
$('#upload').click(function(e) {
var name = $(":file").val();
if (!name) {
alert("First Image Must Be Selected");
e.preventDefault();
}
});
});
&#13;