繁体   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