用jQuery提交后清除表单 [英] Clear form after submission with jQuery

查看:103
本文介绍了用jQuery提交后清除表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刷新后清除此表单的最简单方法是什么?我尝试过的方式将清除表单,但不会提交到数据库。其他人可以向我解释做到这一点的最佳方式。

 < html> 
< head>
< script type =text / javascriptsrc =http://code.jquery.com/jquery-1.4.2.min.js>< / script>
< script type =text / javascriptsrc =http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js>< / script>
< script type =text / javascript>
jQuery(document).ready(function($){
$(#newsletterform)。validate({
debug:false,
rules:{
姓名:必填,
电子邮件:{
required:true,
email:true
}
},
消息:{
姓名:请让我们知道你是谁,
电子邮件:有效的电子邮件将帮助我们与您联系。,
},
submitHandler:function(form) {
//为有效表单创建其他内容
$ .post('newsletter.php',$(#newsletterform)。serialize(),function(data){
$ ('#results').html(data);
});

}

});
});

< / script>

< / head>

< div id =content>
< div id =newsletter-signup>
< h1>注册新闻,更新和优惠!< / h1>
< form id =newsletterformaction =method =post>

< fieldset>

< ul>
< li>
< label for =name>名称:< / label>
< input type =textname =name/>
< / li>
< li>
< label for =email>电子邮件:< / label>
< input type =textname =email/>
< / li>
< div id =results>< div>
< li>
< input type =submitvalue =submitname =signuponclick =/>
< / li>
< / ul>

< / fieldset>

< / form>
< / div>
< / div>
< / html>


解决方案

您可以将此添加到 this.reset();
});

您不能只调用 $('#newsletterform').reset ()是因为 .reset()是一个表单对象,而不是jquery对象,或者是这个效果。您可以在页面的中途阅读此处


What's the easiest way to clear this form after refresh. The way I have tried will clear the form but not submit to the database. Could someone else explain to me the best way to do this.

<html>
<head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
    <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>
    <script type="text/javascript">
    jQuery(document).ready(function($){
        $("#newsletterform").validate({
            debug: false,
            rules: {
                name: "required",
                email: {
                    required: true,
                    email: true
                }
            },
            messages: {
                name: "Please let us know who you are.",
                email: "A valid email will help us get in touch with you.",
            },
            submitHandler: function(form) {
                // do other stuff for a valid form
                $.post('newsletter.php', $("#newsletterform").serialize(), function(data) {
                    $('#results').html(data);
                });

            }

        });
    });

    </script>

</head>

<div id="content">
    <div id="newsletter-signup">
        <h1>Sign up for News, Updates, and Offers!</h1>
        <form id="newsletterform" action="" method="post">

            <fieldset>

                <ul>
                    <li>
                        <label for="name">Name:</label>
                        <input type="text" name="name" />
                    </li>
                    <li>
                        <label for="email">Email:</label>
                        <input type="text" name="email" />  
                    </li>
                        <div id="results"><div>
                    <li>
                        <input type="submit" value="submit" name="signup" onclick="" />         
                    </li>
                </ul>

            </fieldset>

        </form>         
    </div>
</div>
</html>

解决方案

You can add this to the callback from $.post

$( '#newsletterform' ).each(function(){
    this.reset();
});

You can't just call $( '#newsletterform' ).reset() because .reset() is a form object and not a jquery object, or something to that effect. You can read more about it here about half way down the page.

这篇关于用jQuery提交后清除表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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