How to check if any of the Radio buttons are checked or not when clicked on submit button

Pawan

$('#customer_details_table').find("input").each(function(i, obj) {
        if (obj.checked == true) {
            var customer_id = obj.id;
        } else {
            alert('Need to select atlease one radio button');
            event.stopImmediatePropagation();
        }
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table class="table table-striped table-bordered table-hover dataTable" id="customer_details_table" aria-describedby="sample_1_info" style="display: table;">
   <thead>
      <tr>
         <th></th>
         <th>Customer Name</th>
         <th>Address</th>
         <th>Contact No.</th>
         <th>Email</th>
         <th>Action</th>
      </tr>
   </thead>
   <tbody class="odd gradeX">

      <tr>
         <td><label class="radio"><input type="radio" class="bradio" id="20" name="optionsRadios1" value="option1"></label></td>
         <td width="20%">kiran</td>
         <td width="25%">XXXXXXX</td>
         <td width="20%">7654321987</td>
         <td width="20%">[email protected]</td>
         <td width="10%" align="center"><span class="label label-inverse">DeActive</span></td>
      </tr>

      <tr>
         <td><label class="radio"><input type="radio" class="bradio" id="28" name="optionsRadios1" value="option1"></label></td>
         <td width="20%">kiran</td>
         <td width="25%">XXXXXXX</td>
         <td width="20%">9701429843</td>
         <td width="20%">[email protected]</td>
         <td width="10%" align="center"><span class="label label-inverse">DeActive</span></td>
      </tr>

   </tbody>
</table>

When clicked on Activate button i need to check if any of the radio buttons are checked are not ??

Even though i am selecting a radio button , its going to else condition ??

<table class="table table-striped table-bordered table-hover dataTable" id="customer_details_table" aria-describedby="sample_1_info" style="display: table;">
   <thead>
      <tr>
         <th></th>
         <th>Customer Name</th>
         <th>Address</th>
         <th>Contact No.</th>
         <th>Email</th>
         <th>Action</th>
      </tr>
   </thead>
   <tbody class="odd gradeX">

      <tr>
         <td><label class="radio"><input type="radio" class="bradio" id="20" name="optionsRadios1" value="option1"></label></td>
         <td width="20%">kiran</td>
         <td width="25%">XXXXXXX</td>
         <td width="20%">7654321987</td>
         <td width="20%">[email protected]</td>
         <td width="10%" align="center"><span class="label label-inverse">DeActive</span></td>
      </tr>

      <tr>
         <td><label class="radio"><input type="radio" class="bradio" id="28" name="optionsRadios1" value="option1"></label></td>
         <td width="20%">kiran</td>
         <td width="25%">XXXXXXX</td>
         <td width="20%">9701429843</td>
         <td width="20%">[email protected]</td>
         <td width="10%" align="center"><span class="label label-inverse">DeActive</span></td>
      </tr>

   </tbody>
</table>

       <button type="button" id="deactivebtn" class="btn blue" style="display: inline-block;">Activate</button>


$(document).on('click', '#deactivebtn', function(event) {
    $('#customer_details_table').find("input").each(function(i, obj) {
        if (obj.checked == true) {
            var customer_id = obj.id;
        } else {
            alert('Need to select atlease one radio button');
            event.stopImmediatePropagation();
        }
    });

});

Could anybody please help me how to resolve this ??

dfsq

You get "Need to select atlease one radio button" messsage because you are checking all radio inputs, and there is always going to be one unchecked.

Improved version of your code using find(":radio:checked") selector:

$(document).on('click', '#deactivebtn', function(event) {
    var $checked = $('#customer_details_table').find(":radio:checked");
    if (!$checked.length) {
        alert('Need to select atlease one radio button');
        event.stopImmediatePropagation();
    }
    else {
        var customer_id = $checked[0].id;
        alert(customer_id)
    }
});

Demo: http://jsfiddle.net/3ppjfcLa/

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to capture which radio button is checked when page submit ASP.Net MVC

From Dev

keeping radio buttons checked after form submit

From Dev

Radio buttons generated with rails radio_button not changing when clicked

From Dev

calling the same function when any radio button or checkbox are clicked

From Dev

How to check if radio buttons are selected on button click?

From Dev

how to check the radio button is checked or not in windows form

From Dev

How to prevent form submit when a button clicked?

From Dev

How to redirect to homepage when the submit button is clicked?

From Dev

Jquery how to get checked radio button when clicked an list item?

From Dev

How to check if all the radio button gropus in a division are checked or not?

From Dev

how to uncheck a group of radio buttons when one in another group is checked

From Dev

Enable/disable submit button when any/no checkbox in form is checked

From Dev

Jquery check if radio button is checked

From Dev

If radio button checked submit value?

From Dev

PHP check if a radio button is clicked

From Dev

Radio buttons generated with rails radio_button not changing when clicked

From Dev

How to check if radio is checked?

From Dev

How to check if a radio button is clicked?

From Dev

Check that a specific radio button is checked

From Dev

How to prevent form submit when a button clicked?

From Dev

How do I check if a radio button is checked using JavaScript?

From Dev

How to determine if radio buttons are checked?

From Dev

how to check which radio button is checked in android?

From Dev

How to redirect to homepage when the submit button is clicked?

From Dev

Submit form on radio button check

From Dev

How do i check if a specific Radio button is checked among the other radio buttons using Javascript?

From Dev

Check if one of the radio buttons within a div is checked

From Dev

highlight image when clicked on then check a radio button

From Dev

How do I check which radio button is selected when there are multiple radio buttons with the same name? (using jquery)

Related Related

  1. 1

    How to capture which radio button is checked when page submit ASP.Net MVC

  2. 2

    keeping radio buttons checked after form submit

  3. 3

    Radio buttons generated with rails radio_button not changing when clicked

  4. 4

    calling the same function when any radio button or checkbox are clicked

  5. 5

    How to check if radio buttons are selected on button click?

  6. 6

    how to check the radio button is checked or not in windows form

  7. 7

    How to prevent form submit when a button clicked?

  8. 8

    How to redirect to homepage when the submit button is clicked?

  9. 9

    Jquery how to get checked radio button when clicked an list item?

  10. 10

    How to check if all the radio button gropus in a division are checked or not?

  11. 11

    how to uncheck a group of radio buttons when one in another group is checked

  12. 12

    Enable/disable submit button when any/no checkbox in form is checked

  13. 13

    Jquery check if radio button is checked

  14. 14

    If radio button checked submit value?

  15. 15

    PHP check if a radio button is clicked

  16. 16

    Radio buttons generated with rails radio_button not changing when clicked

  17. 17

    How to check if radio is checked?

  18. 18

    How to check if a radio button is clicked?

  19. 19

    Check that a specific radio button is checked

  20. 20

    How to prevent form submit when a button clicked?

  21. 21

    How do I check if a radio button is checked using JavaScript?

  22. 22

    How to determine if radio buttons are checked?

  23. 23

    how to check which radio button is checked in android?

  24. 24

    How to redirect to homepage when the submit button is clicked?

  25. 25

    Submit form on radio button check

  26. 26

    How do i check if a specific Radio button is checked among the other radio buttons using Javascript?

  27. 27

    Check if one of the radio buttons within a div is checked

  28. 28

    highlight image when clicked on then check a radio button

  29. 29

    How do I check which radio button is selected when there are multiple radio buttons with the same name? (using jquery)

HotTag

Archive