Vivek Raj Singh
Vivek Raj Singh

Reputation: 15

Allow only one radiobutton selection in Gridview in each row in asp.net using Javascript

I want to select only one radio button from each row of grid view using JavaScript. I have used 4 radiobutton in each row of Gridview.

I Tried... [1]: https://i.sstatic.net/qp2od.png

<script>
            function GetCheckedRows() {
                var radio1 = null;
                var radio2 = null;
                var radio3 = null;
                var radio4 = null;
                $("#GridView1 tr").each(function () {
                    radio1 = $(this).find(".RadioButton1");
                    if (radio1.is(':checked')) {
                        radio2.checked = false;
                        radio3.checked = false;
                        radio4.checked = false;
                    }
                });
            }
        </script>]

Upvotes: 0

Views: 694

Answers (2)

Vivek Raj Singh
Vivek Raj Singh

Reputation: 15

javascript Code

<script src="https://code.jquery.com/jquery-2.2.4.js"></script>

$(document).on('click','.radio-class',function(){
  $this = $(this);
  // uncheck all other radio inputs except this one
  $('.radio-class').not($this).prop('checked', false);
});

Radio Button

<asp:RadioButton ID="RadioButton1" GroupName="radio-class" runat="server" />

Upvotes: 0

Stinodotbe
Stinodotbe

Reputation: 402

Give each row of radio buttons a different name

<div class="row">
  <input type="radio" name="rowA" /> Label A
  <input type="radio" name="rowA" /> Label B
  <input type="radio" name="rowA" /> Label C
  <input type="radio" name="rowA" /> Label D     
</div>
<div class="row">
  <input type="radio" name="rowB" /> Label A
  <input type="radio" name="rowB" /> Label B
  <input type="radio" name="rowB" /> Label C
  <input type="radio" name="rowB" /> Label D     
</div>

Upvotes: 1

Related Questions