Javascript 确认功能不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7844558/
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
confirm function is not working
提问by SiriusKoder
*<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
我想知道有什么问题。它不起作用,但与http://www.w3schools.com/js/js_popup.asp 中的相同
回答by vol7ron
- You are recursively calling
confirm()
and it's in an infinite loop - You have a
*
at the beginning and end of the document - As kennebec pointed out, you're overwriting
window.confirm
- You have a hanging end
</div>
in the<body>
- 您正在递归调用
confirm()
并且处于无限循环中 - 您
*
在文档的开头和结尾都有一个 - 正如肯尼贝克指出的那样,你正在覆盖
window.confirm
- 你有一个悬端
</div>
的<body>
<html>
<head>
<title>practise</title>
<script type="text/javascript">
function show_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="show_confirm()"/>
</body>
</html>
回答by kennebec
First off, you overwrote window.confirm... Then you call the new confirm, forever, or until there is too much recursion...
首先,你覆盖了 window.confirm ... 然后你永远调用新的确认,或者直到有太多的递归......
回答by nnnnnn
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 call confirm()
which means it is recursively calling itself.
您的函数与内置函数同名window.confirm()
,因此您的函数正在替换该函数。然后在您的函数中调用confirm()
,这意味着它正在递归调用自身。
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'.)
如果您只是将函数重命名为其他名称,它应该可以工作。(例如,您链接到的 w3schools 页面调用其函数“show_confirm”。)
回答by Dave Newton
You need to return the value from the confirm() function, and return thatvalue in the onclick handler if you're trying to use it to prevent the submission.
您需要从 confirm() 函数返回值,如果您试图使用它来阻止提交,则在 onclick 处理程序中返回该值。
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.
不过,IMO 将您的函数命名为“确认”是一个非常糟糕的主意,因为已经有一个方法可以调用该方法,并且您可能会递归,直到浏览器融化。
Also, saying something "doesn't work", without saying what it does and what you expect it to do, makes answering questions difficult.
此外,说一些“不起作用”,而不说它做什么以及你期望它做什么,会使回答问题变得困难。
回答by rahul sharma
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.
confirm()
是 javascript 中的预定义函数,因此您不能将其用作其中一个函数的名称。这是一个错误,重命名函数,它会正常工作。
Also remove the *
from the start and the end.
还要*
从开头和结尾删除。