繁体   English   中英

取消按钮不适用于确认窗口

[英]Cancel button not working for the confirm window

我正在尝试验证JavaScript中的单选按钮,这是我的代码:

if (document.ExamEntry.GCSE.checked == true) {
    confirm("You have selected GCSE. Is this correct?");
}
if (document.ExamEntry.AS.checked == true) {
    confirm("You have selected AS. Is this correct?");
}
if (document.ExamEntry.A2.checked == true) {
    confirm("You have selected A2. Is this correct?");
}

出现确认窗口,然后单击“提交”成功将您带到下一页,但是“取消”按钮似乎不起作用-当我希望它停留在页面上时,它会将您带到下一页,以便您可以进行更改您的选择。

我已经尝试过诸如返回结果之类的事情; 结果=假

它们要么不起作用,要么不起作用,反之亦然,这样取消按钮就可以停留在同一页面上,但是提交按钮也会发生这种情况。

查阅有关确认的文档。 它说,

结果是一个布尔值,指示是选择“确定”还是“取消”(true表示确定)

这意味着您的每一行都应检查返回值。 一种简洁的方法是,例如:

if (!confirm("You have selected A2. Is this correct?")) {
    // event.cancel = true, or whatever you need to do on your side to cancel
} // otherwise fall through and do what you're doing.

因为它是现在,因为你从来不看的返回值confirm ,所以你总是通过落到你的“成功”的情况。

var gcse = true, 
    as   = true,
    a2   = true;

if (document.ExamEntry.GCSE.checked == true) {
    gcse = confirm("You have selected GCSE. Is this correct?"));
}

if (document.ExamEntry.AS.checked == true) {
    as = confirm("You have selected AS. Is this correct?");
}

if (document.ExamEntry.A2.checked == true) {
    a2 = confirm("You have selected A2. Is this correct?");
}

if (gcse && as && a2) {
    // you're golden
    window.location.href = 'otherpage'
}
if (document.ExamEntry.GCSE.checked == true) { 
    if(confirm("You have selected GCSE. Is this correct?")) {
        // do something
    }
} if (document.ExamEntry.AS.checked == true) {
    if(confirm("You have selected AS. Is this correct?")) {
        // do something
    }
}  
if (document.ExamEntry.A2.checked == true) {
    if(confirm("You have selected A2. Is this correct?")) {
        //do something
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM