图片上传后,jQuery自动提交表单-不提交按钮

时间:2019-05-07 09:43:39

标签: javascript php jquery file-upload

我想在图像上传后自动提交表格。表单正在同一页面上处理。所以我没有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);
});

1 个答案:

答案 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]);
  }
}