onsubmit="return false" 是什么意思?(JavaScript,jQuery)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35037069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the meaning of onsubmit="return false"? (JavaScript, jQuery)
提问by Sasa1234
I know that the onsubmit event occurs when a form is submitted.
我知道提交表单时会发生 onsubmit 事件。
Generally what we do is, we are calling a method on onsubmit event like <form action="" onsubmit="myfunction()">
通常我们所做的是,我们在 onsubmit 事件上调用一个方法,例如 <form action="" onsubmit="myfunction()">
Today I saw this, "<form action="" onsubmit="return false">"
. How it works? I could not understand what is the meaning of onsubmit="return false"
.
今天看到这个,"<form action="" onsubmit="return false">"
。这个怎么运作?我不明白是什么意思 onsubmit="return false"
。
PS : I found this when I learning Ajax. It was a tutorial which explains how to submit data to database without refresh the page.
PS:我在学习 Ajax 时发现了这个。这是一个教程,解释了如何在不刷新页面的情况下将数据提交到数据库。
回答by Amar Singh
This basically done to handle the form submission via JavaScript.
这基本上是为了通过 JavaScript 处理表单提交。
For example- for validation purposeSee below code and see how it can be beneficial:
例如-用于验证目的请参阅下面的代码,看看它是如何有益的:
<script language="JavaScript">
myFunctionName() {
if (document.myForm.myText.value == '')
return false;
//when it return false - your form will not submit and will not redirect too
else
return true;
//when it return true- your form will submit and will redirect
// (actually its 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>
回答by Vikas
If you are using 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>