确认功能不起作用

发布于 2024-12-11 09:14:21 字数 616 浏览 0 评论 0原文

*<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 技术交流群。

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

发布评论

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

评论(5

大海や 2024-12-18 09:14:21
  1. 您正在递归调用 confirm() 并且它处于无限循环中
  2. 您在文档的开头和结尾处有一个 *
  3. 正如 kennebec 指出的那样,您正在覆盖 window.confirm
  4. 您在 中有一个悬挂端

http://jsfiddle.net/cvyyL/

<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>
  1. You are recursively calling confirm() and it's in an infinite loop
  2. You have a * at the beginning and end of the document
  3. As kennebec pointed out, you're overwriting window.confirm
  4. You have a hanging end </div> in the <body>

http://jsfiddle.net/cvyyL/

<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>
国产ˉ祖宗 2024-12-18 09:14:21

首先,您覆盖了 window.confirm...
然后你永远打电话给新的确认,
或者直到有太多递归......

First off, you overwrote window.confirm...
Then you call the new confirm, forever,
or until there is too much recursion...

独木成林 2024-12-18 09:14:21

您的函数与内置 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 call confirm() 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'.)

许你一世情深 2024-12-18 09:14:21

您需要从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.

ま昔日黯然 2024-12-18 09:14:21

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.

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