我想在图像上传后自动提交表格。表单正在同一页面上处理。所以我没有if(isset($ _ POST [“ submit”])){
<div class="container">
<h1>jQuery Image Upload
<small>with preview</small>
</h1>
<div class="avatar-upload">
<form id="formImageUpload" action="#" method="post" enctype="multipart/form-data">
<div class="avatar-edit">
<input type='file' id="imageUpload" name="imageUpload" accept=".png, .jpg, .jpeg" onchange="this.form.submit()" />
<label for="imageUpload"></label>
</div>
</form>
<div class="avatar-preview">
<div id="imagePreview" style="background-image: url(http://i.pravatar.cc/500?img=7);" onchange="this.form.submit()" >
</div>
</div>
</div>
</div>
jquery文件是:
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function(e) {
$('#imagePreview').css('background-image', 'url('+e.target.result +')');
$('#imagePreview').hide();
$('#imagePreview').fadeIn(650);
$("#formImageUpload" ).submit();
}
reader.readAsDataURL(input.files[0]);
}
}
$("#imageUpload").change(function() {
readURL(this);
});
答案 0 :(得分:0)
所以,您的答案是这样的:
<div class="container">
<h1>jQuery Image Upload <small>with preview</small></h1>
<div class="avatar-upload">
<form id="formImageUpload" action="#" method="post" enctype="multipart/form-data">
<div class="avatar-edit">
<input type='file' id="imageUpload" name="imageUpload" accept=".png, .jpg, .jpeg" onchange="readURL(this)" />
<label for="imageUpload"></label>
</div>
</form>
<div class="avatar-preview">
<div id="imagePreview" style="background-image: url(http://i.pravatar.cc/500?img=7);" ></div>
</div>
</div>
</div>
JS:
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function(e) {
$('#imagePreview').css('background-image', 'url('+e.target.result +')');
$('#imagePreview').hide();
$('#imagePreview').fadeIn(650);
$("#formImageUpload" ).submit();
}
reader.readAsDataURL(input.files[0]);
}
}