取消表单提交默认行为jQuery Mobile

时间:2012-06-04 02:25:11

标签: php jquery jquery-mobile

我想在不重新加载页面的情况下提交表单,并使用$ .post将数据传递到外部php文件。我在防止在表单提交上重新加载页面时遇到问题。如何在单击提交按钮时取消重新加载?

以下是代码:

<script>
    $(document).ready(function(e){
        $("#formajax").live("submit", function(e){
            var $this = $(this);
            e.preventDefault();
            $.post("process.php", $this.serialize(), function(data){
                alert("DATA PASSED!");
            });
            return false;
        })
    })
</script>

<form method="post" action="" id="formajax">
    <input type="text" id="samplefield" name="samplefield" value=""/>
    <input type="submit" value="Submit"/>
</form>

谢谢!

1 个答案:

答案 0 :(得分:1)

我不熟悉live方法对jQuery的作用,但它似乎是你问题的根源。如果用live替换bind,在我的测试中,页面不会重新加载,我会收到“DATA PASSED”提示。

    <script>
            $(document).ready(function(e){
                    $("#formajax").bind("submit", function(e){
                            var $this = $(this);
                            e.preventDefault();
                            $.post("index.php", $this.serialize(), function(data){
                                    alert("DATA PASSED!");
                            });
                            return false;
                    })
            })
    </script>

    <form method="post" action="" id="formajax">
            <input type="text" id="samplefield" name="samplefield" value=""/>
            <input type="submit" value="Submit"/>
    </form>