使用 jQuery 提交后清除表单 [英] Clear form after submission with jQuery
本文介绍了使用 jQuery 提交后清除表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
刷新后清除此表单的最简单方法是什么.我尝试过的方式将清除表单但不提交到数据库.其他人能否向我解释执行此操作的最佳方法.
<头><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(文档).ready(函数($){$("#newsletterform").validate({调试:假,规则:{名称:必填",电子邮件: {要求:真实,电子邮件:真实}},消息:{name: "请让我们知道你是谁.",电子邮件:有效的电子邮件将帮助我们与您取得联系.",},提交处理程序:函数(表单){//为有效的表单做其他事情$.post('newsletter.php', $("#newsletterform").serialize(), function(data) {$('#results').html(data);});}});});头部><div id="内容"><div id="newsletter-signup"><h1>注册以获取新闻、更新和优惠!</h1><form id="newsletterform" action="" method="post"><字段集><ul><li><label for="name">名称:</label><input type="text" name="name"/><li><label for="email">电子邮件:</label><input type="text" name="email"/><div id="结果"><div><li><input type="submit" value="submit" name="signup" onclick=""/></fieldset></表单>
解决方案
您可以将其添加到 $.post
$( '#newsletterform' ).each(function(){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屋!
查看全文