我正在使用jQuery将表单数据提交到表单操作中的文件。但是,当我提交表单时,浏览器会重定向到action(comment.php)。我不知道这是为什么,因为e.preventDefault()
应该停止浏览器的重定向。理想情况下,我希望浏览器不重定向并保留在当前页面(index.php)上。表单仍然提交onDefaultDefault
的jQuery:
<script type='text/javascript'>
$('document').ready(function(){
$('.commentContainer').load('../writecomment.php');
$('.answerContainer').on('submit', 'form', function(e){
e.preventDefault();
var form = $(this).closest('form');
$.post($(form).attr('action'),
$(form).serialize(),
function() {
$('.commentContainer').load('../writecomment.php');
$('.commentBox').val('');
}
});
});
</script>
HTML表单:
<form method='POST' action='../comment.php'>
//form contents
</form>
将'return false;'加到你的提交函数中。 – Virendra 2011-12-31 19:32:08
@Virendra我应该在哪里添加返回false?在preventDefault之后还是之前? – kirby 2011-12-31 19:33:16
就在最后'});'。 – Virendra 2011-12-31 19:34:18