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.stack.imgur.com/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>]

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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>

Method 2

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x