jQuery - Multiple condition disable checkboxes
This is related to another question I asked but more complex (question here: jQuery - Able to disable checkboxes on condition, but odd problem)
I now have working code that disables checkboxes based on a select-box selection: http://jsfiddle.net/gDHGY/2/
The code in the link above will disable checkboxes based on the grade level. I need to further disable checkboxes based on other checkbox selections. For example, if a checkbox with the "am" and "week1" classes is toggled I need to disable all other "am week1" checkboxes. There's also another class called "full" for full day. Another example being: if a checkbox with classes "full week1" is selected I need to disable both "am week1" and开发者_如何学Go "pm week1" checkboxes. There are 6 weeks of camp total and just the AM, PM, and Full Day options. I really hope that all made sense. If not by all means let me know and I'll try to explain better.
Is it possible to modify the code I have to do this or am I looking at using secondary code to accomplish this?
EDIT #2: Updated the jsFiddle link above... again.
Update #1: Ian's code works great I just need to figure out how to make it run the same functions when the checkboxes are already checked on page load.
Please check on my solution. Hope this helps :)
http://jsfiddle.net/gDHGY/3/
From looking at your code I think what you'll need is to is bind an event to your checkboxes:
$('input[type=checkbox]').click(function() {
var classList = $(this).attr('class').replace(/\s+/g, ".");
if ($(this).attr('checked')) {
$('input.' + classList).attr('disabled', 'true');
$(this).removeAttr('disabled');
} else {
$('input.' + classList).removeAttr('disabled', 'true');
}
});
This won't totally solve your problem as it only disables exactly the same classes (i.e. selecting full week1 won't disable am week1 classes), but some regexps on the classList to change what it's looking for should fix that for you (sorry - would do this myself but the caffeine's not quite kicked in yet this morning)
精彩评论