Saya tidak akan memilih tombol radio lain jika saya sudah memilih satu tombol radio untuk id tombol radio yang berbeda

$('input[type="radio"]').click(function () {
                    if ($(IsCorrectAnswerOption1).is(':checked')) {
                        $("#IsCorrectAnswerOption1").val(true);
                    } else if ($(IsCorrectAnswerOption2).is(':checked')) {
                        $("#IsCorrectAnswerOption2").val(true);
                    }
                    else if ($(IsCorrectAnswerOption3).is(':checked')) {
                        $("#IsCorrectAnswerOption3").val(true);
                    }
                    else if ($(IsCorrectAnswerOption4).is(':checked')) {
                        $("#IsCorrectAnswerOption4").val(true);
                    }
                });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="radio" class="radio-button" name="IsCorrectAnswerOption1" id="IsCorrectAnswerOption1" /> A
<input type="radio" class="radio-button" name="IsCorrectAnswerOption2" id="IsCorrectAnswerOption2" /> B
<input type="radio" class="radio-button" name="IsCorrectAnswerOption3" id="IsCorrectAnswerOption3" /> C
<input type="radio" class="radio-button" name="IsCorrectAnswerOption4" id="IsCorrectAnswerOption4" /> D

Itu juga memilih beberapa tombol radio dan di sini dalam kasus saya, saya perlu menjaga nama juga berbeda

0
Arun 29 Oktober 2019, 12:44

1 menjawab

Jawaban Terbaik

Pertama-tama Anda dapat menghapus centang semuanya, lalu centang yang telah Anda klik

$('input[type="radio"]').click(function () {
  $('input[type="radio"]').prop('checked',false);
  $('input[type="radio"]').val('false');
    $(this).prop('checked','checked');
    $(this).val('true');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="radio" class="radio-button" name="IsCorrectAnswerOption1" id="IsCorrectAnswerOption1" /> A
<input type="radio" class="radio-button" name="IsCorrectAnswerOption2" id="IsCorrectAnswerOption2" /> B
<input type="radio" class="radio-button" name="IsCorrectAnswerOption3" id="IsCorrectAnswerOption3" /> C
<input type="radio" class="radio-button" name="IsCorrectAnswerOption4" id="IsCorrectAnswerOption4" /> D
0
Stender 29 Oktober 2019, 10:19