如何从 mootools 类返回选定的复选框值:
Mootools 类:
var CheckboxGroup = new Class({
Implements: Events,
initialize: function(master, slaves) {
this.master = $(master);
this.slaves = $$(slaves);
this.selected = [];
var that = this;
this.master.addEvent('click', this.onMasterClick.bind(this));
this.slaves.addEvent('click', function() {
that.onSlaveClick(this);
});
},
onMasterClick: function() {
this.slaves.set('checked', this.master.get('checked')).fireEvent('change');
this.fireEvent('masterClick');
this.fireEvent('onCheckboxClick');
},
onSlaveClick: function(slave) {
this.master.set('checked', this.slaves.every(function(el) { return el.get('checked'); }));
this.fireEvent('slaveClick', slave);
this.fireEvent('onCheckboxClick');
},
onCheckboxClick: function() {
??? how to return selected values
}
});
HTML 和脚本:
<div class="container">
<label class="master"><input type="checkbox" id="master" /> Master</label>
<label><input type="checkbox" class="slave" value="1"/> Slave #1</label>
<label><input type="checkbox" class="slave" value="2"/> Slave #2</label>
<label><input type="checkbox" class="slave" value="3"/> Slave #3</label>
<label><input type="checkbox" class="slave" value="4"/> Slave #4</label>
<label><input type="checkbox" class="slave" value="5"/> Slave #5</label>
</div>
<script>
new CheckboxGroup('master', '.slave').addEvents({
'onCheckboxClick': function() {
console.log(this.selected); /*EVERY TIME [] ?????*/
}
});
</script>