[英]What is the meaning of onsubmit="return false"? (JavaScript, jQuery)
I know that the onsubmit event occurs when a form is submitted.我知道提交表单时会发生 onsubmit 事件。
Generally, we are calling a method on the onsubmit event, like <form action="" onsubmit="myfunction()">
.通常,我们在 onsubmit 事件上调用一个方法,例如
<form action="" onsubmit="myfunction()">
。
Today I saw this, "<form action="" onsubmit="return false">"
.今天我看到了这个,
"<form action="" onsubmit="return false">"
。 How does it work?它是如何工作的? I could not understand what is the meaning of
onsubmit="return false"
.我不明白
onsubmit="return false"
的含义是什么。
PS: I found this when learning Ajax. PS:我在学习 Ajax 时发现了这一点。 It was a tutorial which explains how to submit data to a database without refreshing the page.
这是一个教程,它解释了如何在不刷新页面的情况下将数据提交到数据库。
This is basically done to handle the form submission via JavaScript.
这基本上是为了通过 JavaScript 处理表单提交。
For example - for validation purposes例如-用于验证目的
See the below code and see how it can be beneficial:查看下面的代码,看看它是如何有益的:
<script language="JavaScript">
myFunctionName() {
if (document.myForm.myText.value == '')
return false;
// When it returns false - your form will not submit and will not redirect too
else
return true;
// When it returns true - your form will submit and will redirect
// (actually it's a part of submit) id you have mentioned in action
}
</script>
<form name="myForm" onSubmit="return myFunctionName()">
<input type="text" name="myText">
<input type="submit" value="Click Me">
</form>
If you are using a button instead of submit as in my case below:如果您使用按钮而不是提交,就像我下面的情况一样:
<form name="myForm" onSubmit="myFunctionName(); return false">
<input type="text" name="myText">
<input type="button" value="Click Me" onclick="myFunctionName()">
</from>
This effectively prevents the form from being submitted in any circumstances except under script control (via form.submit(), which doesn't trigger a submit event)
<\/blockquote>这有效地防止了表单在任何情况下提交,除了在脚本控制下(通过 form.submit(),它不会触发提交事件)
Reference : Query in Action THIRD EDITION (AFFECTING EVENT PROPAGATION AND SEMANTIC ACTIONS, Page no. 143)参考:Query in Action 第三版(影响事件传播和语义动作,第 143 页)
"
According to the HTML standard<\/a> , onsubmit<\/code> is the name of an event handler<\/a> .
根据
HTML 标准<\/a>,
onsubmit<\/code>是事件处理程序的名称<\/a>。
The event handler content attribute is
treated as a script<\/a> .
事件处理程序内容属性被
视为脚本<\/a>。 This script will be used to process the associated events.
该脚本将用于处理相关事件。 In
step 5 of "The event handler processing algorithm", it says "Process return value as follows"<\/a> :在
“事件处理程序处理算法”的第 5 步中,它说“处理返回值如下”<\/a> :
If event is a BeforeUnloadEvent object and event's type is beforeunload如果事件是 BeforeUnloadEvent 对象并且事件的类型是 beforeunload
![]()
- In this case, the event handler IDL attribute's type will be OnBeforeUnloadEventHandler, so return value will have been coerced into either null or a DOMString.在这种情况下,事件处理程序 IDL 属性的类型将是 OnBeforeUnloadEventHandler,因此返回值将被强制转换为 null 或 DOMString。<\/li>
If return value is not null, then:如果返回值不为空,则:
![]()
- Set event's canceled flag.设置事件的取消标志。<\/li>
If event's returnValue attribute's value is the empty string, then set event's returnValue attribute's value to return value.如果 event 的 returnValue 属性值为空字符串,则设置 event 的 returnValue 属性值为返回值。<\/li><\/ul><\/li><\/ul><\/li>
If special error event handling is true如果特殊错误事件处理为真
![]()
- If return value is true, then set event's canceled flag.如果返回值为真,则设置事件的取消标志。<\/li><\/ul><\/li>
Otherwise否则
![]()
- If return value is false, then set event's canceled flag<\/a> .<\/strong>如果返回值为 false,则设置事件的
取消标志<\/a>。<\/strong><\/li><\/ul><\/li><\/ul><\/blockquote>
So, in short,
return false<\/code> will cancel the event (or in this case, cancels the form submission).
所以,简而言之,
return false<\/code>将取消事件(或者在这种情况下,取消表单提交)。
"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.