此问题通常出现在使用AJAX提交表单时,表单在成功提交后会刷新页面导致元素消失。为避免这种情况,可以使用以下方法:
jQuery.ajax({ type: "POST", url: "submit.php", data: dataString, success: function() { $('#myDiv').html("
"); $('#success').html("We will be in touch soon.
") .hide() .fadeIn(1500, function() { $('#success'); }); } });if (isset($_POST['submit'])) { // submit your form processing code here // then reload the page to avoid resubmission header('Location: '.$_SERVER['PHP_SELF']); exit; }
在HTML中,添加如下代码:
以上两种方法都能避免提交表单后Div消失的情况。