我有这个代码用于将表单数据提交到post_receiver.php
<html>
<head>
<title>jQuery post form data using .post() method by codeofaninja.com</title>
</head>
<body>
<h1>jQuery post form data using .post() method</h1>
<div>Fill out and submit the form below to get response.</div>
<!-- our form -->
<form id='userForm'>
<div><input type='text' name='firstname' placeholder='Firstname' /></div>
<div><input type='text' name='lastname' placeholder='Lastname' /></div>
<div><input type='text' name='email' placeholder='Email' /></div>
<div><input type='submit' value='Submit' /></div>
</form>
<!-- where the response will be displayed -->
<div id='response'></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js "></script>
<script>
$(document).ready(function(){
$('#userForm').submit(function(){
// show that something is loading
$('#response').html("<b>Loading response...</b>");
/*
* 'post_receiver.php' - where you will pass the form data
* $(this).serialize() - to easily read form data
* function(data){... - data contains the response from post_receiver.php
*/
$.post('post_receiver.php', $(this).serialize(), function(data){
// show the response
$('#response').html(data);
}).fail(function() {
// just in case posting your form failed
alert( "Posting failed." );
});
// to prevent refreshing the whole page page
return false;
});
});
</script>
</body>
</html>
此代码没有问题。但我使用Ladda按钮显示提交按钮的旋转效果(这一个:https://msurguy.github.io/ladda-bootstrap/),我希望这个效果会在结果返回后立即停止。
我在这个网站上搜索并找到了类似问题的一些答案,但作为一个几乎不了解Javascript和Ajax的新手,我仍然无法解决我的问题。
所以有人请告诉我我需要插入更多代码。感谢你的所有帮助!
答案 0 :(得分:0)
您应该在l.stop()
之后添加$('#response').html(data);
,但这取决于您如何初始化Ladda。
如果它不起作用,也发布该代码。