确认功能不起作用
*<html>
<head>
<title>practise</title>
<script type="text/javascript">
function confirm() {
var r = confirm("Press the button");
if (r == true) {
alert("You are right");
} else {
alert("You are wrong");
}
}
</script>
</head>
<body>
<input type="button" name="submit" value="showtime" onclick="confirm()"/>
</div>
</body>
</html>*
我想知道出了什么问题。它不起作用,但与 http://www.w3schools.com/js/ 中的相同js_popup.asp
*<html>
<head>
<title>practise</title>
<script type="text/javascript">
function confirm() {
var r = confirm("Press the button");
if (r == true) {
alert("You are right");
} else {
alert("You are wrong");
}
}
</script>
</head>
<body>
<input type="button" name="submit" value="showtime" onclick="confirm()"/>
</div>
</body>
</html>*
I want to know what's the problem. It is not working but it is the same as in http://www.w3schools.com/js/js_popup.asp
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
confirm()
并且它处于无限循环中*
window.confirm
中有一个悬挂端
http://jsfiddle.net/cvyyL/
confirm()
and it's in an infinite loop*
at the beginning and end of the documentwindow.confirm
</div>
in the<body>
http://jsfiddle.net/cvyyL/
首先,您覆盖了 window.confirm...
然后你永远打电话给新的确认,
或者直到有太多递归......
First off, you overwrote window.confirm...
Then you call the new confirm, forever,
or until there is too much recursion...
您的函数与内置
window.confirm()
函数同名,因此您的函数将替换该函数。然后在你的函数中调用confirm(),这意味着它正在递归地调用自身。如果您只需将函数重命名为其他名称,它应该可以工作。 (例如,您链接到的 w3schools 页面调用其函数“show_confirm”。)
Your function has the same name as the built-in
window.confirm()
function, so your function is replacing that one. Then within your function you callconfirm()
which means it is recursively calling itself.It should work if you simply rename your function to something else. (E.g., the w3schools page you link to calls its function 'show_confirm'.)
您需要从confirm() 函数返回该值,如果您尝试使用它来阻止提交,则需要在onclick 处理程序中返回该值。
不过,在我看来,将函数命名为“confirm”是一个非常糟糕的主意,因为已经有一个名为该方法的方法,并且您可能会递归直到浏览器崩溃。
此外,只说某件事“不起作用”,而不说它能做什么以及你期望它做什么,会让回答问题变得困难。
You need to return the value from the confirm() function, and return that value in the onclick handler if you're trying to use it to prevent the submission.
IMO it's a really bad idea to name your function "confirm", though, since there's already a method called that, and you may recurse until the browser melts.
Also, saying something "doesn't work", without saying what it does and what you expect it to do, makes answering questions difficult.
confirm()
是 JavaScript 中的预定义函数,因此您不能将其用作函数之一的名称。这是一个错误,重命名该函数即可正常工作。还要删除开头和结尾的
*
。confirm()
is a predefined function in javascript so you can't use it for the name of one of your functions. It is an error, rename the function and it will work properly.Also remove the
*
from the start and the end.