[英]Jquery ajax php only working once
I have the following jQuery 我有以下jQuery
$("button#submit").click(function(){
$.ajax({
type: "POST",
url: "process.php",
data: $('form.contact').serialize(),
success: function(msg){
$("#form-content").modal('hide');
$("#thanks").html(msg);
$("#thanks").delay(2000).fadeOut("slow");
},
error: function(){
alert("failure");
}
});
});
And Php 和PHP
<?php
if (isset($_POST['name'])) {
$name = strip_tags($_POST['name']);
$email = strip_tags($_POST['Email']);
$sug = strip_tags($_POST['sug']);
echo "<span class='label label-info'>Your Website has been submitted .. Thank you</span>";
}?>
This works the first time and displays the php echo on my page. 这是第一次工作,并在我的页面上显示php echo。 But when I submit the form again it does not show.
但是,当我再次提交表单时,它不会显示。
Your $("#thanks")
dom is hidden. 您的
$("#thanks")
dom隐藏。
$("button#submit").click(function(){
$.ajax({
type: "POST",
url: "process.php",
data: $('form.contact').serialize(),
success: function(msg){
$("#form-content").modal('hide');
$("#thanks").html(msg);
$("#thanks").show(); <----------------ADD THIS
$("#thanks").delay(2000).fadeOut("slow");
},
error: function(){
alert("failure");
}
});
});
使用.on()
。
$(document).on('click','button#submit',function(){ })
use like this 这样使用
$("button#submit").live('click', function(e) {
$.ajax({
type: "POST",
url: "process.php",
data: $('form.contact').serialize(),
success: function(msg){
$("#form-content").modal('hide');
$("#thanks").html(msg);
$("#thanks").delay(2000).fadeOut("slow");
},
error: function(){
alert("failure");
}
});
});
check difference here .live
and .on
jQuery .live() vs .on() method for adding a click event after loading dynamic html 在这里检查差异
.live
和.on
jQuery .live()与.on()方法,以在加载动态html后添加click事件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.