甜蜜警报确认在表单提交时失败



我在html中使用以下代码

<form name = "RegForm" id = "RegForm" class="form-horizontal" method= "post" onsubmit = "return SveFrm();" action = "action.php">

在该函数中,我正在检查错误,发现错误时,我会发出警报:

function SveFrm(){
....
if (err > 0){
swal({ title: "Warning!",   
text: "Some entries are missing.nDo you still want to save this?",   
type: "warning",   
showCancelButton: true,   
confirmButtonColor: "#5cb85c",   
confirmButtonText: "Yes!",   
cancelButtonText: "Cancel!",   
closeOnConfirm: true,   
closeOnCancel: true }, 
function(isConfirm){   
if (isConfirm) { 
return true; 
} else {
event.preventDefault();
event.stopPropagation(); 
return false;
} 
});
}
}

遇到错误时,警报会闪烁一秒钟,然后自动提交表单,而无需等待任何用户输入。我的代码中没有其他可捕获的错误。显然,我的代码有问题。请帮我纠正它。

删除这些代码行 你不能使用它,你的函数中没有对event的引用

event.preventDefault();
event.stopPropagation(); 

停止提交表单的默认操作

<form onsubmit="event.preventDefault(); SveFrm();">

如果一切正常,请提交表格

if (isConfirm) { 
document.forms['RegForm'].submit();
}

最新更新