[英]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.