jQuery表单提交

时间:2011-05-16 09:30:04

标签: jquery jquery-mobile

当我在下面提交表单时,刷新当前页面并将表单参数附加到页面URL的末尾。这是为什么会发生的?我需要提交表单,但保持表单参数隐藏。

由于

<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;
            });

1 个答案:

答案 0 :(得分: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;
  });
});