Javascript jQuery在为true时禁用选项
嗨,我想做一个简单的测验。我不熟悉jQuery,所以如果它看起来很愚蠢,我道歉 但它不起作用。如果用户选择q1b…正确答案…jQuery将禁用其余单选按钮 html格式:Javascript jQuery在为true时禁用选项,javascript,jquery,radio-button,Javascript,Jquery,Radio Button,嗨,我想做一个简单的测验。我不熟悉jQuery,所以如果它看起来很愚蠢,我道歉 但它不起作用。如果用户选择q1b…正确答案…jQuery将禁用其余单选按钮 html格式: <div class="quiz"> <ul class="no-bullet" id="question1"> <li><input type="radio" name="
<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>
您可以尝试以下操作:为
注册单击事件处理程序。回答单选按钮,并在其中使用.prop()禁用所有其他同级单选按钮
- 不正确
- 正确
- 不正确
您正在删除disabe属性:必须使用$('input[type='radio'id=“q1a”和&“q1c”)。prop('disabled',true)
非常好用。谢谢,它看起来很随机。使用特定的标识符没有必要吗?是的,使用特定的标识是一种很好的做法,但要确保ID在整个DOM中都是唯一的。我已经发布了一些代码,这些代码将适用于所有具有相同结构的单选按钮,比如如果你有多个这样的问题,那么就使用相同的代码一切都会好的。很高兴帮助你:)
$(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");
}
});
});
$(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);
});
});