简体   繁体   English

onsubmit="return false" 是什么意思? (JavaScript, jQuery)

[英]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)这有效地防止了表单在任何情况下提交,除了在脚本控制下(通过 form.submit(),它不会触发提交事件)

<\/blockquote>

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> :

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM