在Html.BeginForm中使用onsubmit参数中的return的目的是什么?

在ASP.NET MVC剃刀视图中,我单击按钮时提交一个表单。如果我在onsubmit参数上指示“返回”,则其行为与如果我不指示“返回”的行为不同。

因此,如果我按如下所示设置BeginForm的参数:

html.beginform("","",FormMethod.Post,new { id = "myForm",onsubmit = "return doaction();",@class = "well" })

其行为与以下行为不同:

html.beginform("",onsubmit = "doaction();",@class = "well" })

那么使用return或不使用return的目的是什么?

heeroj 回答:在Html.BeginForm中使用onsubmit参数中的return的目的是什么?

onsubmit = "doAction();"

doAction(){
   alert("Hello World!");
}

^这将只是执行您的函数,并且由于doAction()不会阻止事件执行默认过程,因此表单将提交。


onsubmit = "return doAction();"

doAction(){
   alert("Hello World!");
   return false; // form will not submit
   // return true; // form will submit
}

^这将执行您的函数,并且如果您的函数返回一个false布尔值,它将阻止表单提交。


要查看它们的确切区别,请尝试onsubmit="doAction()"和返回false的函数;

onsubmit = "doAction();"

doAction(){
   alert("Hello World!");
   return false; // the form will still submit
}

该表单仍将提交,因为尽管函数返回了return,但您未指定false关键字。 return关键字将向表单发送信号,以首先检查该函数返回的值。

本文链接:https://www.f2er.com/2379746.html

大家都在问