问题
I have an Ajax form in my application.I want to pass the event to OnBegin
function and then use event.preventdefault()
which will stop the form from submitting and then on checking some conditions I'm trying to submit the form manually.But it's not working and I'm not able to make out why.
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:
function OnBeginForm(e) {
e.preventDefault();
if(some condition){
$('#Ajax_form').submit();
}
else{
return false;
}
}
回答1:
event.preventdefault() which will stop the form from submitting
You can also use return false;
stop the form from submitting, it basically does the effect of event.preventdefault()
.
Check this.
Below script should work for you:
function OnBeginForm() {
if (some condition) {
return false;
}
}
Hope helps :)
Reference
回答2:
My be i found a better solution....
$('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');
}
});
来源:https://stackoverflow.com/questions/39742804/is-it-possible-to-pass-event-to-an-onbegin-function-of-an-ajax-form