JavaScript 'onclick' 事件 'return' 关键字功能
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7814949/
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
JavaScript 'onclick' event 'return' keyword functionality
提问by Shiroi98
I am really new to javascript, and stumbled upon the return
keyword. Basically, what is the difference in terms of these 2 statements?
我对 javascript 真的很陌生,偶然发现了这个return
关键字。基本上,这两个陈述有什么区别?
<input type="checkbox" onclick="doAlert()" />
vs
对比
<input type="checkbox" onclick="return doAlert();" />
Essentially, both returned the same results and called the function, but is there more to it? Any help greatly appreciated :). Thanks!
本质上,两者都返回了相同的结果并调用了函数,但还有更多吗?非常感谢任何帮助:)。谢谢!
采纳答案by Y. Shoham
Returning false from the function, will abort the effect of the checking. Because the native of functions that written hardcoded into html properties (it became some new local function), writing the html without the word "return" will just run the function, and lose its returning value, as if you've wrote:
从函数返回 false,将中止检查的效果。因为将硬编码到 html 属性中的函数的本机(它变成了一些新的本地函数),编写不带“返回”一词的 html 只会运行该函数,并丢失其返回值,就好像您已经写过一样:
function doAlert() {
if(some_condition)
return false;
else
return true;
}
function some_local_function() {
doAlert();
}
Function some_local_function
won't return any value, although doAlert
returns.
函数some_local_function
不会返回任何值,尽管doAlert
返回。
When you write "return", it's like you wrote the second function like this:
当你写“return”时,就像你写了第二个函数一样:
function some_local_function() {
return doAlert();
}
which preserves the returning value of doAlert, whatever it will be. If it's true - the action will perform (the checkbox will be checked) - otherwise - it will cancel.
它保留了 doAlert 的返回值,无论它是什么。如果为真 - 操作将执行(复选框将被选中) - 否则 - 将取消。
You can see live expamle here: http://jsfiddle.net/RaBfM/1/
您可以在此处查看实时示例:http: //jsfiddle.net/RaBfM/1/
回答by Glenn Ferrie
Some html elements have JS events that behave differently when true/false is returned. For instance:
某些 html 元素具有在返回 true/false 时表现不同的 JS 事件。例如:
<input type='submit' value='Click Me' onSubmit='ValidateForm();'>
...vs...
……对……
<input type='submit' value='Click Me' onSubmit='return ValidateForm();'>
In the second instance, if the ValidateForm
function returned false the form will not submit, in the first even if the function returns false the form will still submit.
在第二种情况下,如果ValidateForm
函数返回 false 表单将不会提交,在第一种情况下,即使函数返回 false 表单仍将提交。
I think this scenario, you can see the different between using the return
keyword and not.
我认为这种情况下,您可以看到使用return
关键字和不使用关键字之间的区别。
UPDATEDTo simplify, if you use the return
keyword you are passing a value back to the function that called the onsubmit
. Without it, you are simply calling the function that you name in the event handler and returning nothing.
更新为简化起见,如果您使用return
关键字,您会将值传递回调用onsubmit
. 没有它,您只需调用您在事件处理程序中命名的函数并且不返回任何内容。
回答by Arun Raaj
adding return
to a function(), it disables the default behaviour of the browser, it disables the job of submit
. Means it will keep you on the same page if the function returns false and you can fill up the value into the field again.
添加return
到 function() 中,它会禁用浏览器的默认行为,它会禁用submit
. 意味着如果函数返回 false 它将让您保持在同一页面上,您可以再次将该值填充到该字段中。
If you do not use return
with the function() then submit
function will perform its job, it will redirect to the next specified page, without caring about the returned result whether it was true or false.
如果不return
与 function() 一起使用,则submit
function 将执行其工作,它将重定向到下一个指定页面,而不关心返回的结果是 true 还是 false。