每当我提交表单时,我的这个表单都会被重定向到"process_save.php"。我想要的是仍然能够在"process_sav.php"上执行函数,但在提交表单后不能重定向到那里。相反,关闭表单(在模态中)并重新加载我在当前页面上的表。
如何防止提交后重定向?
<form id="form1" action="process_save.php" method="post">
....
</form>
$('form1').submit(function(event) {
$.ajax({
type : 'POST',
dataType : 'json',
url : "process_save.php",
data : $(this).serialize(),
encode : true
})
});
您还可以删除submit,并使用按钮调用ajax。
<form id="form1" action="process_save.php" method="post">
.....
<button type="Button" id="save">Save</button>
</form>
$('#save').click(function(event) {
$.ajax({
type : 'POST',
dataType : 'json',
url : "process_save.php",
data : $('#form1').serialize(),
encode : true
})
});
添加event.preventDefault() ;
jQuery('#form1').on('submit',function(event) {
event.preventDefault() ;
$.ajax({
type : 'POST',
dataType : 'json',
url : "process_save.php",
data : $(this).serialize(),
encode : true
})
});