问题描述
我知道提交表单时会发生 onsubmit 事件.
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()">
.
今天看到这个,"<form action=""onsubmit="return false">"
.它是如何工作的?我不明白 onsubmit="return false"
是什么意思.
Today I saw this, "<form action="" onsubmit="return false">"
. How does it work? I could not understand what is the meaning of onsubmit="return false"
.
PS:我在学习 Ajax 时发现了这个.这是一个教程,解释了如何在不刷新页面的情况下将数据提交到数据库.
PS: I found this when learning Ajax. It was a tutorial which explains how to submit data to a database without refreshing the page.
推荐答案
例如 - 用于验证目的
查看下面的代码,看看它有什么好处:
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>
这篇关于onsubmit="return false"是什么意思?(JavaScript,jQuery)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!