javascript radio button return previous checked value

zachary adams

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/>
LTJD

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] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

If radio button is checked not running

분류에서Dev

checked first radio button as default using javascript only

분류에서Dev

Javascript - Need to un-gray a textbox/dropdown list when a certain radio button is checked

분류에서Dev

Show whether dynamic radio button is checked or not

분류에서Dev

show a field if only a certain radio button is checked

분류에서Dev

Checked radio button style sibling label

분류에서Dev

How to check radio button with radio button value

분류에서Dev

jQuery display div on pageload depending on which radio button is checked

분류에서Dev

Radio button default checked not working because of JQuery function

분류에서Dev

Radio and Checkbox Checked Values Won't Show in JavaScript

분류에서Dev

iCheck library: value of selected radio button

분류에서Dev

jQuery if condition for radio button name and value

분류에서Dev

How to pass radio button value with php

분류에서Dev

Jquery for pass selected radio button value on button click

분류에서Dev

how to enable and disable a dropdown list based on whether a radio button is checked or not through jstl in jsp

분류에서Dev

jQuery textbox velue multiply and show on another text box on Radio button checked

분류에서Dev

jQuery mobile radio button changed event always fires even if already checked

분류에서Dev

How to assign the variable value to radio button's value in php

분류에서Dev

Radio button clicked to slide & show hidden div & pass a value

분류에서Dev

How to get selected radio button value onclick from php

분류에서Dev

How to check a Radio button from the value of a table of mySQL

분류에서Dev

Setting value on dynamic radio button with ng-repeat

분류에서Dev

Get selected button's value from Polymer radio group with jquery

분류에서Dev

console out checked radio buttons

분류에서Dev

check all radio groups are checked

분류에서Dev

Angular radio buttons are not checked correctly

분류에서Dev

jQuery or JavaScript Validation required if yes radio button is chosen w/ exceptions

분류에서Dev

How to automatically compute Radio button and Checkbox values using javascript

분류에서Dev

What would my return value be in order to continue from a previous statement?

Related 관련 기사

  1. 1

    If radio button is checked not running

  2. 2

    checked first radio button as default using javascript only

  3. 3

    Javascript - Need to un-gray a textbox/dropdown list when a certain radio button is checked

  4. 4

    Show whether dynamic radio button is checked or not

  5. 5

    show a field if only a certain radio button is checked

  6. 6

    Checked radio button style sibling label

  7. 7

    How to check radio button with radio button value

  8. 8

    jQuery display div on pageload depending on which radio button is checked

  9. 9

    Radio button default checked not working because of JQuery function

  10. 10

    Radio and Checkbox Checked Values Won't Show in JavaScript

  11. 11

    iCheck library: value of selected radio button

  12. 12

    jQuery if condition for radio button name and value

  13. 13

    How to pass radio button value with php

  14. 14

    Jquery for pass selected radio button value on button click

  15. 15

    how to enable and disable a dropdown list based on whether a radio button is checked or not through jstl in jsp

  16. 16

    jQuery textbox velue multiply and show on another text box on Radio button checked

  17. 17

    jQuery mobile radio button changed event always fires even if already checked

  18. 18

    How to assign the variable value to radio button's value in php

  19. 19

    Radio button clicked to slide & show hidden div & pass a value

  20. 20

    How to get selected radio button value onclick from php

  21. 21

    How to check a Radio button from the value of a table of mySQL

  22. 22

    Setting value on dynamic radio button with ng-repeat

  23. 23

    Get selected button's value from Polymer radio group with jquery

  24. 24

    console out checked radio buttons

  25. 25

    check all radio groups are checked

  26. 26

    Angular radio buttons are not checked correctly

  27. 27

    jQuery or JavaScript Validation required if yes radio button is chosen w/ exceptions

  28. 28

    How to automatically compute Radio button and Checkbox values using javascript

  29. 29

    What would my return value be in order to continue from a previous statement?

뜨겁다태그

보관