所以我在下面有这个javascript代码。每当用户在确认框上点击取消时,我都会尝试这样做。他们选择的先前选择的选项将被重新选中。另一种选择,而不是之前的选择,如果我们可以根据价值选择选项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/>
答案 0 :(得分:1)
如果让函数返回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/