Is it possible to pass event to an OnBegin function of an Ajax form?

☆樱花仙子☆ 提交于 2019-12-11 00:01:19

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!