这是代码
//Script for questions where you check one option or the other (locks other options out)
$('.optionBox input').click(function(){
var optionBoxElement$ = $(this).closest('.optionBox');
//If no option is checked, the make all the options available to be selected
//Otherwise, one option must be checked so lock out all other options
if(optionBoxElement.find('input:not(:checked)').length == optionBoxElement.find(':input').length)
optionBoxElement.find(':input').prop('disabled',false);
else
optionBoxElement.find('input:not(:checked)').prop('disabled',true);
optionBoxElement.find('input:checked').prop('disabled',false); //makes sure that the checkbox that was checked is not disabled so the user can uncheck and change his answer
});
答案 0 :(得分:5)
你可以像下面这样做。您所要做的就是检查复选框是否已选中。
$('.optionBox input:checkbox').click(function(){
var $inputs = $('.optionBox input:checkbox');
if($(this).is(':checked')){ // <-- check if clicked box is currently checked
$inputs.not(this).prop('disabled',true); // <-- disable all but checked checkbox
}else{ //<-- if checkbox was unchecked
$inputs.prop('disabled',false); // <-- enable all checkboxes
}
})
答案 1 :(得分:1)
//Script for questions where you check one option or the other (locks other options out)
$(':checkbox').click(function(){
var $checkbox = $(this);
var isChecked = $checkbox.is(':checked')
//If no option is checked, the make all the options available to be selected
//Otherwise, one option must be checked so lock out all other options
if(isChecked)
$checkbox.siblings(":checkbox").attr("disabled", "disabled");
else
$checkbox.siblings(":checkbox").removeAttr("disabled");
});
答案 2 :(得分:1)
或许像this fiddle这样的东西。
$('.optionBox :checkbox').click(function() {
var $checkbox = $(this), checked = $checkbox.is(':checked');
$checkbox.closest('.optionBox').find(':checkbox').prop('disabled', checked);
$checkbox.prop('disabled', false);
});