简体   繁体   English

javascript单选按钮返回先前的检查值

[英]javascript radio button return previous checked value

So I have this javascript code below. 所以我下面有这个JavaScript代码。 I am trying to make it so whenever the user hits cancel on the confirmation box. 我试图做到这一点,以便每当用户在确认框中单击取消时。 The previous selected option they have selected becomes reselected. 他们选择的先前选择的选项将重新选择。 Another option instead of the previous, is if we could have it select option 2 based on value ' A' instead of my current way of selecting option 2 based on the array of radio names. 另一个选择,而不是以前的选择是,如果我们可以让它基于值'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/>

If you let your function return false, the event is aborted and the radio button group returns to previous state. 如果让函数返回false,则事件将中止,单选按钮组将返回到以前的状态。

Edit: use return in the onclick attribute as well. 编辑:在onclick属性中也使用return。

Html 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 使用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/ jsFiddle = http://jsfiddle.net/ahsjoe0x/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM