JavaScript“onclick”事件“返回”关键词功能

发布于 2024-12-10 19:01:29 字数 298 浏览 0 评论 0原文

我对 javascript 很陌生,偶然发现了 return 关键字。基本上,这两种说法有什么区别?

<input type="checkbox" onclick="doAlert()" />

vs

<input type="checkbox" onclick="return doAlert();" />

本质上,两者都返回相同的结果并调用该函数,但是还有更多吗?非常感谢任何帮助:)。谢谢!

I am really new to javascript, and stumbled upon the return keyword. Basically, what is the difference in terms of these 2 statements?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我还不会笑 2024-12-17 19:01:29

某些 html 元素具有在返回 true/false 时表现不同的 JS 事件。例如:

<input type='submit' value='Click Me' onSubmit='ValidateForm();'>

...vs...

<input type='submit' value='Click Me' onSubmit='return ValidateForm();'>

在第二个实例中,如果 ValidateForm 函数返回 false,则表单将不会提交,在第一个实例中,即使函数返回 false,表单仍会提交。

我认为在这种情况下,您可以看到使用 return 关键字和不使用 return 关键字之间的区别。

更新为了简化,如果您使用return关键字,您会将一个值传递回调用onsubmit的函数。如果没有它,您只需调用您在事件处理程序中命名的函数,并且不会返回任何内容。

更新 2021-01-21 此功能也适用于 html 锚点/链接 (a) 上的 onclick 方法:

示例用法:

<a href="#never-used" onclick="alert('click clack'); return false;" > 

Click Me

Some html elements have JS events that behave differently when true/false is returned. For instance:

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

I think this scenario, you can see the different between using the return keyword and not.

UPDATED To 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 do not return anything.

UPDATE 2021-01-21 This functionality also work for the onclick method on html anchors / links (a):

Sample Usage:

<a href="#never-used" onclick="alert('click clack'); return false;" > 

Click Me

坚持沉默 2024-12-17 19:01:29

从函数返回 false,将中止检查的效果。因为本机函数被硬编码到 html 属性中(它变成了一些新的本地函数),所以编写没有“return”一词的 html 只会运行该函数,并丢失其返回值,就像您编写的那样:

function doAlert() {
   if(some_condition)
     return false;
   else
     return true;
}
function some_local_function() {
   doAlert();
}

Function <尽管 doAlert 返回,但 code>some_local_function 不会返回任何值。

当你写“return”时,就像你写了第二个函数一样:

function some_local_function() {
   return doAlert();
}

它保留了 doAlert 的返回值,无论它是什么。如果为真 - 操作将执行(复选框将被选中) - 否则 - 它将取消。

您可以在此处查看实时说明:http://jsfiddle.net/RaBfM/1/

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:

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.

When you write "return", it's like you wrote the second function like this:

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.

You can see live expamle here: http://jsfiddle.net/RaBfM/1/

难得心□动 2024-12-17 19:01:29

添加return到一个function(),它会禁用浏览器的默认行为,它会禁用submit的工作。意味着如果函数返回 false,它将使您保持在同一页面,并且您可以再次将值填充到字段中。

如果你没有将return与function()一起使用,那么submit函数将执行它的工作,它将重定向到下一个指定的页面,而不关心返回的结果是否是是真是假。

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文