0

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

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

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

它们要么不起作用,要么如果起作用,则反之亦然,因此取消按钮通过停留在同一页面上起作用,但提交按钮也会发生这种情况。

4

3 回答 3

3

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

result 是一个布尔值,指示是否选择了 OK 或 Cancel(true 表示 OK)

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

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,因此您总是陷入“成功”案例。

于 2013-12-11T21:55:45.360 回答
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
    }
}
于 2013-12-11T21:57:17.663 回答
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'
}
于 2013-12-11T21:54:42.413 回答