2013-12-11 76 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