Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/438.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 是否可以将事件传递给Ajax表单的OnBegin函数?_Javascript_Jquery_Asp.net Mvc_Forms_Ajax.beginform - Fatal编程技术网

Javascript 是否可以将事件传递给Ajax表单的OnBegin函数?

Javascript 是否可以将事件传递给Ajax表单的OnBegin函数?,javascript,jquery,asp.net-mvc,forms,ajax.beginform,Javascript,Jquery,Asp.net Mvc,Forms,Ajax.beginform,我的应用程序中有一个Ajax表单。我想将事件传递到OnBegin函数,然后使用event.preventdefault(),这将阻止表单提交,然后在检查某些条件时,我尝试手动提交表单。但它不起作用,我无法解释原因 Ajax.beginfom: @using (Ajax.BeginForm("Update", "Project", false, new AjaxOptions { InsertionMode = InsertionMode.Replace,

我的应用程序中有一个Ajax表单。我想将事件传递到
OnBegin
函数,然后使用
event.preventdefault()
,这将阻止表单提交,然后在检查某些条件时,我尝试手动提交表单。但它不起作用,我无法解释原因

Ajax.beginfom:

   @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"/> 
}
function OnBeginForm(e) {
   e.preventDefault();
   if(some condition){
     $('#Ajax_form').submit();
   }
   else{
     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');
        }


    });
event.preventdefault()将停止表单提交

您还可以使用
返回false
停止表单提交,它基本上起到了
event.preventdefault()
的作用。 检查

下面的脚本应该适合您:

function OnBeginForm() {

    if (some condition) {
      return false;
    }

}
希望有帮助:)


我们无法触发提交。这将导致一个无限循环,我们将不得不使用('submit')手动提交表单。无论如何,我要感谢你和+1。@JibinBalachandran,你在调试时遇到无限循环了吗?这会创建一个无限循环,因为
$('Ajax_form').trigger('submit')
触发submit事件,该事件依次调用
OnBeginForm()执行代码行,则取消提交。(尽管如此,我始终无法理解当您使用
$.ajax()
方法获得更大的灵活性和更好的性能时,为什么用户想要使用
AjaxBegin()
(甚至不再受支持)@JibinBalachandran,因为
e
不是提交事件(实际上它已经被插件取消了)