您好,我正在尝试创建一个简单的测验。我是 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/