我的应用程序中有一个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()会阻止表单提交
你也可以使用return false;
来阻止表单提交,它基本上起到了event.preventdefault()
的作用。检查这个。
下面的脚本应该为您工作:
function OnBeginForm() {
if (some condition) {
return false;
}
}
希望有所帮助:)
参考我找到了一个更好的解决方案....
$('input[type=submit]').on('click', function (e) {
e.preventDefault();
var buttonid = $(this).attr('id');
if (buttonid == "btnUpdate") {
//Do what ever you want it fires before Ajax Submit
//Finaly ajax submit manually trigger submit event
$(this).trigger('submit');
}
});