onsubmit =“return false”的含义是什么? (JavaScript,jQuery) [英] What is the meaning of onsubmit="return false"? (JavaScript, jQuery)

查看:240
本文介绍了onsubmit =“return false”的含义是什么? (JavaScript,jQuery)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



通常我们要做的是,我们在onsubmit事件上调用一个方法,比如< form action =onsubmit =myfunction()>



今天我看到< form action =onsubmit =return false>。怎么运行的?我无法理解 onsubmit =return false是什么意思。



PS:我学习Ajax。这是一篇介绍如何在不刷新页面的情况下向数据库提交数据的教程。 基本上完成了手动处理表单提交。


例如 -

$ b $ < SCRIPT LANGUAGE =JavaScript> $ gt;
请参阅下面的代码, ;
myFunctionName(){
if(document.myForm.myText.value =='')
return false;
//当它返回false时 - 表单不会提交,也不会重定向
else
return true;
//当它返回true时,您的表单将提交并将重定向
//(实际上它是提交的一部分)您在操作中提到的id
}
< / SCRIPT>

< FORM NAME =myFormonSubmit =return myFunctionName()>
< INPUT TYPE =TEXTNAME =myText>
< INPUT TYPE =SUBMITVALUE =点击我>
< / FORM>


I know that the onsubmit event occurs when a form is submitted.

Generally what we do is, we are calling a method on onsubmit event like <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".

PS : I found this when I learning Ajax. It was a tutorial which explains how to submit data to database without refresh the page.

解决方案

This basically done to manually handle the form submission.

For example - for validation purpose See 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>

这篇关于onsubmit =“return false”的含义是什么? (JavaScript,jQuery)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆