Javascript 使用 jQuery 提交后清除表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8701812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Clear form after submission with jQuery
提问by Kelbizzle
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>
回答by Andrew Hymanman
You can add this to the callback from $.post
您可以将其添加到回调中 $.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 hereabout half way down the page.
你不能只调用$( '#newsletterform' ).reset()
因为.reset()
是一个表单对象而不是一个 jquery 对象,或者类似的东西。你可以阅读更多关于它这里约一半下来的页面。
回答by Amritpal singh
You can reset your form with:
您可以使用以下方法重置表单:
$("#myform")[0].reset();
回答by kiran kumar
Better way to reset your form with jQuery is Simply trigger
a reset
event on your form.
使用 jQuery 重置表单的更好方法只是表单上的trigger
一个reset
事件。
$("#btn1").click(function () {
$("form").trigger("reset");
});
回答by Dau
回答by mas-designs
Propably this would do it for you.
可能这会为你做。
$('input').val('').removeAttr('checked').removeAttr('selected');
回答by Jerry King
A quick reset of the form fields is possible with this jQuery reset function.
$(selector)[0].reset();
使用此 jQuery 重置功能可以快速重置表单字段。
$(selector)[0].reset();
回答by Harshit Khatri
Just add this to your Action file in some div or td, so that it comes with incoming XML object
只需在某些 div 或 td 中将此添加到您的 Action 文件中,以便它带有传入的 XML 对象
<script type="text/javascript">
$("#formname").resetForm();
</script>
Where "formname" is the id of form you want to edit
其中“formname”是您要编辑的表单的 id