试试这个http://jsfiddle.net/Q4Gyq/
HTML
<select id="sel">
<option value="0">A class</option>
<option value="1">B class</option>
</select>
<div id="opts">
Food: <input type="checkbox" id="ifood" value="food" class="0 1" />
Water: <input type="checkbox" id="iwater" value="water" class="0 1"/>
Cable TV: <input type="checkbox" id="itv" value="tv" class="1" style="display: none" />
Elevator: <input type="checkbox" id="ielev" value="elevator" class="1" style="display: none" />
</div>
Javascript
function stateManager(optsDiv){
var _optsWrapper = '';
var _states = {};
var _currentState = null;
var construct = function(optsDiv){
_optsWrapper = optsDiv;
}
this.setStateId = function(stateId){
_currentState = stateId;
}
this.save = function(stateToSet){
_states[_currentState] = [];
var checkedCheckBoxes = $(_optsWrapper).find('input[type=checkbox]:checked');
for(var i = 0; i < checkedCheckBoxes.length; i++){
_states[_currentState].push($(checkedCheckBoxes[i]).attr('id'));
}
this.setStateId(stateToSet);
}
this.getState = function(stateId){
return _states[stateId] ? _states[stateId] : [];
}
construct(optsDiv);
}
window.onload = function(){
var manager = new stateManager('#opts');
manager.setStateId($('#sel').val());
$('#sel').on('change', function(){
manager.save(this.value);
//extra functionality {
filterCheckboxes(this.value);
clearStates();
//extra functionality }
var state = manager.getState(this.value);
for(var i = 0; i < state.length; i++){
$('#' + state[i]).prop('checked', true);
}
});
}
function filterCheckboxes(cls){
var checkboxes = $('input[type=checkbox]');
for(var i = 0; i < checkboxes.length; i++){
$(checkboxes[i]).hasClass(cls) ? $(checkboxes[i]).show() : $(checkboxes[i]).hide();
}
}
function clearStates(){
var checkboxes = $('input[type=checkbox]:checked');
for(var i = 0; i < checkboxes.length; i++){
$(checkboxes[i]).prop('checked', false);
}
}