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

时间:2013-12-11 21:51:51

标签: javascript jquery radio-button confirm validating

我正在尝试验证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?");
}

显示确认窗口并单击“提交”成功将您带到下一页,但是取消按钮似乎不起作用 - 当我希望它保留在页面上时它会带您到下一页你可以改变你的选择。

我尝试过返回结果等事情;                            result = false

它们要么不起作用,要么它们不起作用,那么反之亦然,因此取消按钮的工作原理是停留在同一页面上,但这也会发生在提交按钮上。

3 个答案:

答案 0 :(得分:3)

查看confirm上的文档。它说,

  

result是一个布尔值,表示是选择了OK还是Cancel(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的返回值,所以你总是会看到你的“成功”案例。

答案 1 :(得分:0)

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'
}

答案 2 :(得分:0)

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
    }
}