图像上传后JQuery自动提交表单 - 不提交按钮

I would like to auto-submit form after image upload. The form is being processed on the same page. so i dont have 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 file is:

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);
});
dongzhun6952
dongzhun6952 onchange=“readURL(this)”做到了
一年多之前 回复
duankuang7928
duankuang7928 你能否从行动中删除#:<formid=“formImageUpload”action=“#”method=“post”enctype=“multipart/form-data”>然后尝试提交
一年多之前 回复
douzi7711
douzi7711 我认为调用onchange=“readURL(this)”会对你有用。
一年多之前 回复

1个回答

So, your answer is like this:

<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]);
  }
}
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐