0

I have a page in which there are 2 radio buttons and a next button. I have made next button disabled and it is enabled only when I select any radio button. But now when I go to another page and come back to this page the next button comes as disabled although the radio button is already selected. PFB the code

$(document).ready(function () {
     $('#commandButton_1_0').attr('disabled', 'true');
     $('input:radio[name=a_SignatureOption]').click(function () {
         var checkval = $('input:radio[name=a_SignatureOption]:checked').val();
         if (checkval == '1' || checkval == '2') {
             $('#commandButton_1_0').removeAttr('disabled');
         }
     });
});
Arun Unnikrishnan
  • 2,339
  • 2
  • 25
  • 39
Neha Jain
  • 3
  • 1
  • 1
  • 5

8 Answers8

1

Try

$(document).ready(function() {
    $('input:radio[name=a_SignatureOption]').click(function() {
        var checkval = $('input:radio[name=a_SignatureOption]:checked').val();
        $('#commandButton_1_0').prop('disabled', !(checkval == '1' || checkval == '2'));
    });
});

Demo: Fiddle

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
0

On load of document you need to check whether radio button is clicked or not

$(document).ready(function() {
  $('input:radio[name=a_SignatureOption]').each(function() {
         checked(this);
  });

  $('input:radio[name=a_SignatureOption]').click(function() {
    checked(this);
  });

  function checked(obj){
    if($(obj).is(':checked')) {
        $('#commandButton_1_0').removeAttr('disabled');
    }else{
        $('#commandButton_1_0').attr('disabled', 'true');
    }
  }
});
Shaunak D
  • 20,588
  • 10
  • 46
  • 79
Beena Shetty
  • 3,676
  • 2
  • 28
  • 31
0

I took time to understand your problem,

This can be solved by unchecking the radio while loading the page.

$(document).ready(function () {
    $('input:radio[name=a_SignatureOption]').prop('checked', false);
    $('#commandButton_1_0').attr('disabled', 'true');  
    $('input:radio[name=a_SignatureOption]').click(function () {
    var checkval = $('input:radio[name=a_SignatureOption]:checked').val();
    if (checkval == '1' || checkval == '2') {
        $('#commandButton_1_0').removeAttr('disabled');
    }
});
});

check out JSFiddle, btw redirect to other site and press back button to find the difference.

Hope you understand.

Praveen
  • 55,303
  • 33
  • 133
  • 164
0

// Try this.............

$(document).ready(function () {
$("input:radio[name=a_SignatureOption]").removeAttr('checked'); 
$('#commandButton_1_0').attr("disabled","disabled");

 $('input:radio[name=a_SignatureOption]').click(function () {
    var checkval = $('input:radio[name=a_SignatureOption]:checked').val();   
     if (checkval == '1' || checkval == '2') {

         $('#commandButton_1_0').removeAttr("disabled","disabled");
     }
 });

});

pardeep
  • 21
  • 3
0

It is simple as your question is

<form name="urfrm">
<input type="radio" value="1" name="a_SignatureOption"> Yes<br>
<input type="radio" value="2" name="a_SignatureOption"> No<br>
<input type="submit" id="butn" name="butn" value="next" disabled><br>
</form>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
    //This will check the status of radio button onload
    $('input[name=a_SignatureOption]:checked').each(function() {
        $("#butn").attr('disabled',false);
    });

    //This will check the status of radio button onclick
    $('input[name=a_SignatureOption]').click(function() {
        $("#butn").attr('disabled',false);
    });
});
</script>
Lepanto
  • 1,413
  • 1
  • 8
  • 15
0

The easiest solution I can think of - albeit somewhat belatedly, is:

// selects all input elements, whose name is 'a_SignatureOption' and whose
// type is 'radio'
// binds a change event-handler, using 'on()':
$('input[name=a_SignatureOption][type="radio"]').on('change', function(){
    // sets the 'disabled' property of the button to 'true' if zero radio inputs
    // are checked, or to false if one is checked:
    $('#commandButton_1_0').prop('disabled', $('input[name=a_SignatureOption][type="radio"]:checked').length === 0);
// triggers the change event-handler on page load:
}).change();

References:

David Thomas
  • 249,100
  • 51
  • 377
  • 410
0

You forgot to check the buttons at the beginning.

$(document).ready(function () {
    $('#commandButton_1_0').attr('disabled', 'true');
    if( $('input[name=a_SignatureOption]:checked' ).size() > 0 ) {
       $('#commandButton_1_0').removeAttr('disabled');
    }
    $('input[name=a_SignatureOption]').click(function () {
        if( $('input:radio[name=a_SignatureOption]:checked' ).size() > 0 ) {
            $('#commandButton_1_0').removeAttr('disabled');
        }
    });
});

By the way, I always suggest to add classes to form elements and work with those, instead of using [name="..."]. It's quicker and simplier and you can change input names (if necessary) without touching js

RiccardoC
  • 876
  • 5
  • 10
0
    <html>
<head>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>

<script>
    $(document).ready(function () {
        $('#commandButton_1_0').attr('disabled', 'true');

        $('input:radio[name=a_SignatureOption]').click(function () {
            var checkval = $('input:radio[name=a_SignatureOption]:checked').val();
            alert(checkval)
            if (checkval == '1' || checkval == '2') {
                $('#commandButton_1_0').removeAttr('disabled');
            }
            else {
                $('#commandButton_1_0').attr('disabled', 'disabled');
            }
        });
    });
</script>
</head>
<body>
  <input type="radio" name="a_SignatureOption" value="1" /> value1
    <br />
    <input type="radio" name="a_SignatureOption" value="2"/> value2
    <br />
    <input type="radio" name="a_SignatureOption" value="3" checked="checked"/> value3
    <br />
    <input type="button" id="commandButton_1_0" value="Next"/>
</body>
</html>
Jobelle
  • 2,717
  • 1
  • 15
  • 26