jQuery.post刷新我的页面? [英] jQuery.post refreshes my page?

查看:144
本文介绍了jQuery.post刷新我的页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的页面上有一个表单,代码如下。但是,当我点击提交,似乎我的网页刷新。

I have the following code with a form on my page. But when I click submit, it seemed like my page refreshes.

形式:

form:

<form action='#' method='post'>
    <div><input type='text' name='name' /></div>
    <div><input type='submit' value='Save' /></div>
</form>

JS:

<script type='text/javascript'>
    $(document).ready(function() {
        $('form').bind('submit',function() {
            var str = $('form').serialize();

            $.post("save.php", { formString: str }, 
                function(data) {
                    alert("Saved: " + data);
                }
            );

        });
    });
</script>

非常感谢您的帮助!

Thanks a lot for any help!

推荐答案

设置一个

return false;

添加到您的表单中(例如onSubmit)。你的表单由jQuery($ .post)和页面本身发布,因为你不会停止这样做。另一个选项(原理相同)是让jquery-part禁用表单的默认行为:

to your form (for example on the onSubmit). Your form is sended by both jQuery ($.post) and the page itself, because you do not stop it to do that. Another option (works the same) is to let the jquery-part disable the default behaviour of your form:

//your code
$('form').bind('submit',function() {
   // more of your code
   return false;
}

这篇关于jQuery.post刷新我的页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆