2014-02-06 33 views
1

這裏提交表單是我的形式jQuery的變化事件中使用AJAX

<form name="uploadImg" id="uploadImg" class="profile-image" enctype="multipart/form-data"> 
     <input type="file" name="profile" id="updProfileImg"> 
</form> 

,這裏是我的jQuery事件

$("#updProfileImg:file").change(function() { 
    $('#uploadImg').submit(function() { 
    var queryString = new FormData($('form')[0]); 
    $.ajax({ 
     type: "POST", 
     url: 'index.php?route=account/edit/upload', 
     data: queryString, 
     contentType: false, 
     processData: false, 
     beforeSend: function() { 
     }, 
     success: function() { 
     } 
    }) 
}) 
}) 

但變化事件不會觸發表單提交,所以我嘗試trigger('submit')但頁面刷新,而不是提交在Ajax中。

+0

嘗試切換返回功能? –

回答

0

您正在錯誤地綁定事件。正如您目前所擁有的,更改字段將觸發提交的綁定。它必須是這樣的:

// bind the submit event 
$('#uploadImg').submit(function() { 
    var queryString = new FormData($('form')[0]); 
    $.ajax({ 
     type: "POST", 
     url: 'index.php?route=account/edit/upload', 
     data: queryString, 
     contentType: false, 
     processData: false, 
     beforeSend: function() { 
     }, 
     success: function() { 
     } 
    }); 
}); 

// bind the change event to trigger a submit 
$("#updProfileImg:file").change(function() { 
    $("#uploadImg").submit(); 
}); 
0

一個簡單的改裝工程

$("#updProfileImg:file").change(function() { 
     //$('#uploadImg').submit(function() { 
      var queryString = new FormData($('#uploadImg')[0]); 
      $.ajax({ 
       type: "POST", 
       url: 'index.php?route=account/edit/upload', 
       data: queryString, 
       contentType: false, 
       processData: false, 
       beforeSend: function() { 

       }, 
       success: function() { 

       } 
      }) 
     //}) 
    }) 
0

你應該試試這個代碼:

$("#updProfileImg:file").on("change", function(){ 
    var queryString = new FormData($('#uploadImg')[0]); 
    $.ajax({ 
     type: "POST", 
     url: 'index.php?route=account/edit/upload', 
     data: queryString, 
     contentType: false, 
     processData: false, 
     beforeSend: function() {}, 
     success: function() {} 
    }) 
}); 

,因爲我想到了 「.change()」 將在第一次改變時開了一次。