2014-06-29 28 views
1

我被困在一個項目上,我在大學裏做了一個項目,我試圖做的確認工作返回相同的東西,當你點擊'確定'和'取消' 我希望它取消選中的單選按鈕,當我按取消,我希望它保持單選按鈕,勾選當我按OK單選按鈕確認,取消和確定做同樣的事情

<script language="javascript" type="text/javascript"> 
function getConfirmation() 
{ 
    var retVal = confirm("Your Level of Entry is A2, is this correct?"); 
    if (retVal == true) 
    { 
     alert("Your Level of Entry is A2!"); 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 

回答

2

設置按鈕的checked屬性:

function getConfirmation() 
{ 
    var retVal = confirm("Your Level of Entry is A2, is this correct?"); 
    if (retVal) 
    { 
     alert("Your Level of Entry is A2!"); 
     return true; 
    } 
    else 
    { 
     document.getElementById('radioButton').checked = false; 
     return false; 
    } 
} 
0

你的JS as it is工程。

雖然沒有任何內容會改變單選按鈕的狀態。

爲此,您可以使用checked屬性。

假設您有這樣的:

<input type="radio" name="group1" checked="true" id="a2"/>A2 
<input type="radio" name="group1" id="not"/>Not A2 

如果確認取消你可以在你的else塊檢查其他單選按鈕添加document.getElementById("not").checked = true;

function getConfirmation() { 
    var retVal = confirm("Your Level of Entry is A2, is this correct?"); 
    if (retVal) { 
     alert("Your Level of Entry is A2!"); 
     return true; 
    } else { 
     document.getElementById("not").checked = true; 
     return false; 
    } 
} 

Demo

相關問題