javascript - true 时 jQuery 禁用选项

标签 javascript jquery radio-button

您好,我正在尝试创建一个简单的测验。我是 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="answer"><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);
 });
});

关于javascript - true 时 jQuery 禁用选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29278147/

相关文章:

javascript - 为什么 jQueryeach() 函数中的全局变量会破坏我的翻转效果?

javascript - 如果未选择,如何验证此选择选项?

android:button = 单选按钮中的@null

javascript - 如何将 JavaScript 函数的返回值作为 asp-route 值传递?

javascript - 在 onclick 函数中传递 id

javascript - 如何使用jquery获取一行中的列

jquery - Backbone.js 在 Web 应用程序中适合什么位置

objective-c - 如何在 cocoa 应用程序中对单选按钮(来自 Storyboard)进行分组?

javascript - 如何制作多个具有相同名称的单选按钮组?

javascript - jQuery - 获取跨度中单个字母的偏移量