2011-05-16 124 views
1

當我提交下面的表單時,刷新當前頁面並將表單參數追加到頁面URL的末尾。爲什麼會發生這種情況?我需要提交表單,但隱藏了表單參數。jQuery表單提交

感謝

<div class="ui-block-b"><button id="submit" type="submit">Submit</button></div> 

      $("#submit").click(function(){ 

       var formData = $("#newPostForm").serialize(); 

       $.ajax({ 
        type: "POST", 
        url: "/mobile/newpost.php", 
        cache: false, 
        data: formData, 
        success: onSuccess 
       }); 

       return false; 
      }); 
+0

將按鈕類型從'type =「submit」'更改爲'type =「button」',它不會刷新頁面。 – 2011-05-16 09:44:55

回答

1

擺脫type="submit"(其中由何必實際上是<input type="submit">)的,只是使它成爲一個標準的按鈕,如果你不打算實際提交表單:

<input type="button" id="submit" value="Submit" /> 

的另一種方法是設置在表格上的onsubmit處理程序:

$(document).ready(function() { 
    $("#newPostForm").submit(function(){ 

    var formData = $("#newPostForm").serialize(); 

    $.ajax({ 
     type: "POST", 
     url: "/mobile/newpost.php", 
     cache: false, 
     data: formData, 
     success: function(data) { 
     window.location.href = "/where/to/go"; 
     } 
    }); 

    return false; 
    }); 
}); 
+0

謝謝,所以一旦表單提交後重定向,應該進入成功:onSuccess函數調用? – 2011-05-16 10:00:55

+0

@ user470184查看關於如何實現成功重定向的更新內聯代碼 – 2011-05-16 10:02:52

+0

我正在考慮嘗試重定向到url:「/mobile/newpost.php」。我只想檢查我提交的參數。 – 2011-05-16 10:16:03