FormData没有提交按钮

时间:2016-04-19 20:09:51

标签: javascript jquery forms form-data

嗨,我有上传文件的表格

$("#form").on('submit',(function(e) {
        e .preventDefault();
        $.ajax({
            url: "index2.php",
            type: "POST",
            data:  new FormData(this),
            contentType: false,
            cache: false,
            ...

格式为html:

 <form id="form" action="index2.php" method="post" enctype="multipart/form-data">
  <input id="uploadImage" type="file" accept="image/*" name="image" /><br/>
  <input id="button" type="submit" value="Upload">
 </form>

它有效。但是我想删除按钮。我想在选择文件时自动上传。

          $.ajax({
                url: "index2.php",
                type: "POST",
                data:  new FormData("#form"),
                contentType: false,

                 ...

但不行。能帮我做那个吗?

2 个答案:

答案 0 :(得分:1)

您可以添加放置区域(只需拖放)。看看AutoUpload:true。

$('.fileupload').fileupload({
  dataType: 'json',
  url: "/somewhere.com/here",
  dropZone: $("#picturezone"),
  autoUpload: true,
  done: function (e, data) {
   $("#pic").attr("src", URl + data.result.name + "?" + d.gettime());
 }
 }).on('fileuploadprogressall', function (e, data) {
  //Do something while it is uploading.
 });

答案 1 :(得分:0)

您只需要监听该输入的onChange事件。

您的代码应该是这样的:

$('#uploadImage').on('change',function(){
    $.ajax({
            url: "index2.php",
            type: "POST",
            data:  new FormData("#form"),
            contentType: false,

             ...
    });
});