$("#time-slot input[type='checkbox']").click(function() {
// Disable all unchecked
$('input[type=checkbox]:not(:checked)').attr('disabled','disabled');
// If 3 are checked we are done
if($('input[type=checkbox]:checked').length == 3)
return;
// If the checkbox either side of this one is checked, we only have 1 option
if($(this).prev().is(':checked') && $(this).next().is(':checked')){
$(this).removeAttr('disabled');
return;
}
// Enable the checkboxes before and after the current selection
$('input[type=checkbox]:checked').prev().removeAttr('disabled');
$('input[type=checkbox]:checked').next().removeAttr('disabled');
// If none are checked, enable all
if($('input[type=checkbox]:checked').length == 0)
$('input[type=checkbox]').removeAttr('disabled');
});
это должно охватывать все возможности. Смотрите это в действии здесь ...
http://jsfiddle.net/JcCdD/
UPDATE
Разница здесь в том, что мы должны использовать prevAll и nextAll, чтобы пропустить элементы br и label. Так как они работают немного по-другому при использовании набора элементов, мы должны разбить его на каждый оператор.
$("#time-slot input[type='checkbox']").click(function() {
// Disable all unchecked
$('input[type=checkbox]:not(:checked)').attr('disabled','disabled');
// If 3 are checked we are done
if($('input[type=checkbox]:checked').length == 3)
return;
// If the checkbox either side of this one is checked, we only have 1 option
if($(this).prevAll('input').first().is(':checked') && $(this).nextAll('input').first().is(':checked')){
$(this).removeAttr('disabled');
return;
}
// Enable the checkboxes before and after the current selection
$('input[type=checkbox]:checked').each(function(){
$(this).prevAll('input').first().removeAttr('disabled');
$(this).nextAll('input').first().removeAttr('disabled');
});
// If none are checked, enable all
if($('input[type=checkbox]:checked').length == 0)
$('input[type=checkbox]').removeAttr('disabled');
});
новый пример здесь ... http://jsfiddle.net/bEjTf/