确认取消按钮对这个简单的代码不起作用



确认/取消按钮不工作,我不知道是什么问题

即使在点击确认框取消后,它仍然引导我到另一个页面。

<html>
<head>
<title>password</title>
</head>
<body>
<script type="text/javascript">
function f() {
if (myform.password.value == "") {
window.alert("you need to enter your password");
} else {
window.confirm("are you sure u want to proceed");
window.location =' https://www.facebook.com/';
}
}
</script>
<center>
<h1>confirm password page</h1>
<form id="myform">
Enter the Password
<input type="password" id="password" value="">
<input type="button" id="continue" value ="continue" onclick="f()">
</center>
</form>
</body>
</html>

你可以做

if(window.confirm("are you sure u want to proceed")) {
// Proceed to next page
}

confirm()方法显示一个带有消息、OK按钮和Cancel按钮的对话框。

如果用户单击了"ok",则confirm()方法返回true,否则返回false。

Window.confirm ()返回一个布尔值.

如果<<ul>
  • strong>好click thentrue
  • 如果<<li> strong> 然后点击false

    。所以你必须像if-else块那样处理。

    if(window.confirm("are you sure u want to proceed")){
    // your redirection logic here.
    }
    

    表单的默认行为导致跳转到另一个页面。为了防止这种行为,您应该在表单上使用preventDefault()。如:

    event.preventDefault();
    

    下面是一个示例:

    https://codepen.io/navab/pen/YzemrWB

    复制粘贴我的代码当你按取消键时它会返回给你

    <html>
    <head>
    <title>password</title>
    </head>
    <body>
    <script type="text/javascript">
    function f() {
    if (myform.password.value == "") {
    window.alert("you need to enter your password");
    } else {
    if(window.confirm("are you sure u want to proceed")==true){
    window.location =' https://www.facebook.com/';}
    else{
    window.location.reload();
    }
    }
    }
    </script>
    <center>
    <h1>confirm password page</h1>
    <form id="myform">
    Enter the Password
    <input type="password" id="password" value="">
    <input type="button" id="continue" value ="continue" onclick="f()">
    </form>
    </center>
    </body>
    </html>
    

    我改了

    function f(){
    if(myform.password.value==""){
    window.alert("you need to enter your password");
    }
    else{
    if (window.confirm("are you sure u want to proceed")) {
    window.location =' https://www.facebook.com/';
    }
    }
    }
    <center>
    <h1>confirm password page </h1>
    <form id="myform">
    Enter the Password<input type="password" id="password" value="">
    <input type="button" id="continue" value ="continue" onclick="f()">
    </form>
    </center>

    相关内容

    • 没有找到相关文章

    最新更新