2013-12-11 51 views
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?"); 
} 

确认画面显示出来,然后点击“提交”,成功地把你带到下一个页面,但取消按钮似乎不起作用 - 当我希望它保留在页面上时,它会将您带到下一页,以便您可以更改选项。

我尝试过的东西,如返回结果; 结果= false

他们要么不工作,要么他们这样做,反之亦然,这样取消按钮的工作方式就是停留在同一页面上,但这也会发生在提交按钮上。

+2

下一页是什么?你是在谈论下一个确认框,还是你在某个地方重定向? – adeneo

+3

您的确认对确认结果不做任何处理。他们也可能是警报。 –

+0

你会想要跟踪确认的结果,并用它做点什么... –

回答

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

查看confirm的文档。它说,

结果是指示是否正常或选择取消(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返回值,所以你总是通过你的“成功”的情况下下降。

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

有人可以美化我的答案! – vrunoa

+0

谢谢@ScottMermelstein – vrunoa