问题
I am using for form submit. I would like to know how can I clear form fields and print a success message after form submission.
I used var options = ( clearForm: true }
but it didn't work
My code :-
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
<script src="http://malsup.github.com/jquery.form.js"></script>
<script>
// wait for the DOM to be loaded
$(document).ready(function() {
// bind 'myForm' and provide a simple callback function
$('#myForm').ajaxForm(function() {
//alert("Thank you for your comment!");
});
});
</script>
</head>
<body>
<form id="myForm" name="myForm" action="comment.php" method="post" enctype="multipart/form-data">
<input type="text" name="name" />
<br />
<textarea name="comment"></textarea>
<br />
<input type="file" value="Share a Pic" name="file" id="file" />
<br />
<input type="submit" value="Submit Comment" />
</form>
</body>
回答1:
Just guessing, but would'nt the native form.reset()
do that ?
$(document).ready(function() {
$('#myForm').ajaxForm(function() {
this.reset();
alert('success');
});
});
回答2:
You can use resetForm
for this.
Here is the docs http://malsup.com/jquery/form/#options-object
or cna use functions like resetForm
and clearForm
docs on http://malsup.com/jquery/form/#api
Or try simply:
$(document).ready(function() {
$('#myForm').ajaxForm(function() {
success:function(){
$('#myForm').reset();
}
});
});
回答3:
Try using clearForm();
also resetForm();
.
Official Document : http://malsup.com/jquery/form/#api
<script type="text/javascript">
$(document).ready(function() {
// bind 'myForm' and provide a simple callback function
$('#myForm').ajaxForm(function() {
//alert("Thank you for your comment!");
$(this).clearForm();
});
});
</script>
回答4:
Should be :
$(document).ready(function() {
$("#myForm").submit(function(){
$("input[type='text']").val('');
$("input[type='textarea']").val('');
alert('Message');
});
});
回答5:
It worked with the following code :-
<script type="text/javascript">
$(document).ready(function() {
var options = {
clearForm: true,
resetForm: true
};
// bind 'myForm' and provide a simple callback function
$('#myForm').ajaxForm(function() {
//alert("Thank you for your comment!");
});
$('#myForm').ajaxForm(options);
});
</script>
Thanks for the help
来源:https://stackoverflow.com/questions/15085937/how-to-clear-form-fields-and-print-a-success-message-after-ajax-form-submission