【发布时间】:2011-07-10 21:13:45
【问题描述】:
我正在尝试使用 jQuery 选中/取消选中所有复选框。现在通过选中/取消选中父复选框,所有子复选框都被选中/取消选中,父复选框的文本也被更改为选中/取消选中。
现在我想用输入按钮替换父复选框,并将按钮上的文本也更改为选中/取消选中。这是代码,有人可以调整代码吗?
$( function() {
$( '.checkAll' ).live( 'change', function() {
$( '.cb-element' ).attr( 'checked', $( this ).is( ':checked' ) ? 'checked' : '' );
$( this ).next().text( $( this ).is( ':checked' ) ? 'Uncheck All' : 'Check All' );
});
$( '.cb-element' ).live( 'change', function() {
$( '.cb-element' ).length == $( '.cb-element:checked' ).length ? $( '.checkAll' ).attr( 'checked', 'checked' ).next().text( 'Uncheck All' ) : $( '.checkAll' ).attr( 'checked', '' ).next().text( 'Check All' );
});
});
<input type="checkbox" class="checkAll" /> <b>Check All</b>
<input type="checkbox" class="cb-element" /> Checkbox 1
<input type="checkbox" class="cb-element" /> Checkbox 2
<input type="checkbox" class="cb-element" /> Checkbox 3
【问题讨论】: