【问题标题】:Javascript Return False works in html, not headJavascript Return False 适用于 html,而不是 head
【发布时间】:2015-06-22 04:28:05
【问题描述】:

“Return False”似乎只在内联工作,而不是在文档中。

这行得通:

<html><body><form action="index.html" method="post">
<input type="submit" onClick="alert('Hello'); return false;">
</form>
</body></html>

这不起作用:

<html><head>
function myfunction(){
alert("Hello");
return false;
}
</head>
<body>
<form action="index.html" method="post">
<input type="submit" onClick="myfunction();">
</form>
</body></html>

【问题讨论】:

  • 你还没有发布什么有效?
  • 为什么会出人意料?在第一种情况下,您从事件处理程序返回一些东西,而在第二种情况下,您没有。您从 myfunction 返回,但不是从事件处理程序返回。

标签: javascript html javascript-events


【解决方案1】:

您错过了问题的“这有效”部分,但我假设您的意思是说 onclick='alert("Hello");返回错误;'。如果是这样,要使第二种情况起作用,您必须将函数的值传递给父元素的 onclick 事件。所以基本上,onclick='return myFunction()';

【讨论】:

    猜你喜欢
    • 2022-11-13
    • 1970-01-01
    • 2018-06-26
    • 1970-01-01
    • 2015-03-27
    • 1970-01-01
    • 1970-01-01
    • 2010-12-21
    • 1970-01-01
    相关资源
    最近更新 更多