我的应用程序中有一个 Ajax 表单。我想将事件传递给 OnBegin
函数,然后使用 event.preventdefault()
这将阻止表单提交然后在检查一些条件时,我尝试手动提交表单。但它不起作用,我无法弄清楚为什么。
Ajax.BeginFrom:
@using (Ajax.BeginForm("Update", "Project", false, new AjaxOptions { InsertionMode = InsertionMode.Replace,
HttpMethod = "Post",
OnBegin = "return OnBeginForm(event);",
OnSuccess = "OnSuccessArtwork(data);",
OnFailure = "OnFailureArtwork(data);" },
new { id = "Ajax_form" }))
{
// Here I have all the form elements //
<input type="submit" id="btnUpdate" class="btn02 pull-left" value="Update"/>
}
OnBegin 函数:
function OnBeginForm(e) {
e.preventDefault();
if(some condition){
$('#Ajax_form').submit();
}
else{
return false;
}
}
最佳答案
event.preventdefault() which will stop the form from submitting
您还可以使用return false;
停止表单提交,它基本上起到event.preventdefault()
的作用。
检查this .
下面的脚本应该适合您:
function OnBeginForm() {
if (some condition) {
return false;
}
}
希望有帮助:)
关于javascript - 是否可以将事件传递给 Ajax 表单的 OnBegin 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39742804/