当为true时,jQuery禁用选项

jQuery disable option when true

本文关键字:选项 jQuery true 当为      更新时间:2023-09-26

嗨,我正在尝试创建一个简单的测验。我是jQuery的新手,所以如果它看起来很愚蠢,我很抱歉。

但它不起作用。如果用户选择q1b。。。正确答案。。。jQuery将禁用剩余的单选按钮。

html表单:

           <div class="quiz">
                     <ul class="no-bullet" id="question1">
                        <li><input type="radio" name="q1" id="q1a"> <label for="q1a">Incorrect</label></li>
                        <li class="answerswer"><input type="radio" name="q1" id="q1b"> <label for="q1b">Correct</label></li>
                        <li><input type="radio" name="q1" id="q1c"> <label for="q1c">Incorrect</label></li>
                    </ul>
                </div>

和jQuery:

$(document).ready(function(){
//If user selects q1b  which is the correct answer...
//all other radio buttons will be disabled
$('#question1').click(function() {
   if($('#q1b').is(':checked'))
       { 
       //disables radio button q1a and radio button q1c
          $("input[type='radio' id="q1a" && "q1c").removeAttr("disabled"); 
       }
     });
 });

您可以尝试以下操作:为.answer单选按钮注册点击事件处理程序,并在其中使用.prop() 禁用所有其他同级单选按钮

$(document).ready(function(){
  //If user selects q1b  which is the correct answer...
  //all other radio buttons will be disabled
  $('li.answer input[type="radio"]').click(function() {
     //disable all other radio button
 $(this).closest('ul').find('input[type="radio"]').not(this).prop('disabled',true);
 });
});

    $(document).ready(function(){
    $('li.answer input[type="radio"]').click(function() {
     $(this).closest('ul').find('input[type="radio"]').not(this).attr('disabled',true);
     });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <div class="quiz">
                         <ul class="no-bullet" id="question1">
                            <li><input type="radio" name="q1" id="q1a"> <label for="q1a">Incorrect</label></li>
                            <li class="answerswer"><input type="radio" name="q1" id="q1b"> <label for="q1b">Correct</label></li>
                            <li><input type="radio" name="q1" id="q1c"> <label for="q1c">Incorrect</label></li>
                        </ul>
                    </div>