簡體   English   中英

javascript單選按鈕返回先前的檢查值

[英]javascript radio button return previous checked value

所以我下面有這個JavaScript代碼。 我試圖做到這一點,以便每當用戶在確認框中單擊取消時。 他們選擇的先前選擇的選項將重新選擇。 另一個選擇,而不是以前的選擇是,如果我們可以讓它基於值'A'選擇選項2,而不是我當前基於無線電名稱數組選擇選項2的方式。

<script type="text/javascript">
function confirmUnschedule() {
    var checked = false;
    var element = "";
    var inputs = document.getElementsByName('Acceptance');
        for (var i = 0; i < inputs.length; i++) {
          if (inputs[i].checked) {
           checked = true;
           element = inputs[i];
           break;
          }
        }   
    if(checked==true){
        if (!confirm('Scheduling will be undone if you change the rating. Are you Sure?')){
          inputs[1].checked=true;
        };
    };
   }
</script>

<input type='radio' name='Acceptance' value=' ' checked='checked' onclick='confirmUnschedule()'>option 1
<br/>
<input type='radio' name='Acceptance' value=' A' onclick='confirmUnschedule()'>option 2
<br/>
<input type='radio' name='Acceptance' value=' R' onclick='confirmUnschedule()'>option 3
<br/>

如果讓函數返回false,則事件將中止,單選按鈕組將返回到以前的狀態。

編輯:在onclick屬性中也使用return。

HTML

<input type='radio' name='Acceptance' value=' ' checked='checked' onclick='return confirmUnschedule();'>option 1
<br/>
<input type='radio' name='Acceptance' value=' A' onclick='return confirmUnschedule();'>option 2
<br/>
<input type='radio' name='Acceptance' value=' R' onclick='return confirmUnschedule();'>option 3
<br/>

使用Javascript

function confirmUnschedule() {

    var checked = false;
    var element = "";
    var inputs = document.getElementsByName('Acceptance');

    for (var i = 0; i < inputs.length; i++) {
        if (inputs[i].checked) {
            checked = true;
            element = inputs[i];
            break;
        }
    }
    if (checked === true) {
        if (!confirm('Scheduling will be undone if you change the rating. Are you Sure?')) {
            return false;
        }
    }
}

jsFiddle = http://jsfiddle.net/ahsjoe0x/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM