So I have this javascript code below. 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.
<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.
Edit: use return in the onclick attribute as well.
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/
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다