阿里妈妈,帮你实现网络赚钱梦,流量变成现金!
Javascript实现复选框的全选反选这些功能的代码网上有很多,这里要记录的是用JQuery来实现的,可以少写很多代码。JQuery还是很强大的~~~步骤一:HTML代码
复制内容到剪贴板
程序代码
程序代码<div class="controls">
<span><input type="checkbox" class="checkAll" /> <b>Check All</b> <span> or
<span><a href="javascript:void(0);" class="invertSelection">Invert Selection</a></span>
</div>
<div class="elements">
<span><input type="checkbox" class="cb-element" /> Checkbox 1</span>
<span><input type="checkbox" class="cb-element" /> Checkbox 2</span>
<span><input type="checkbox" class="cb-element" /> Checkbox 3</span>
<span><input type="checkbox" class="cb-element" /> Checkbox 4</span>
</div>
<span><input type="checkbox" class="checkAll" /> <b>Check All</b> <span> or
<span><a href="javascript:void(0);" class="invertSelection">Invert Selection</a></span>
</div>
<div class="elements">
<span><input type="checkbox" class="cb-element" /> Checkbox 1</span>
<span><input type="checkbox" class="cb-element" /> Checkbox 2</span>
<span><input type="checkbox" class="cb-element" /> Checkbox 3</span>
<span><input type="checkbox" class="cb-element" /> Checkbox 4</span>
</div>
步骤二:JQuery代码,放到head标签里
复制内容到剪贴板
程序代码
程序代码<script type="text/javascript" src="http://code.jquery.com/jquery-latest.pack.js" language="javascript"></script>
<script type="text/javascript" language="javascript">
$( function() {
$( '.checkAll' ).live( 'change', function() {
$( '.cb-element' ).attr( 'checked', $( this ).is( ':checked' ) ? 'checked' : '' );
$( this ).next().text( $( this ).is( ':checked' ) ? 'Uncheck All' : 'Check All' );
});
$( '.invertSelection' ).live( 'click', function() {
$( '.cb-element' ).each( function() {
$( this ).attr( 'checked', $( this ).is( ':checked' ) ? '' : 'checked' );
}).trigger( 'change' );
});
$( '.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' );
});
});
</script>
<script type="text/javascript" language="javascript">
$( function() {
$( '.checkAll' ).live( 'change', function() {
$( '.cb-element' ).attr( 'checked', $( this ).is( ':checked' ) ? 'checked' : '' );
$( this ).next().text( $( this ).is( ':checked' ) ? 'Uncheck All' : 'Check All' );
});
$( '.invertSelection' ).live( 'click', function() {
$( '.cb-element' ).each( function() {
$( this ).attr( 'checked', $( this ).is( ':checked' ) ? '' : 'checked' );
}).trigger( 'change' );
});
$( '.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' );
});
});
</script>
原文链接: http://www.webdeveloperjuice.com/2010/03/15/check-all-uncheck-all-and-invert-checkboxes-using-jquery/
发表评论
上一篇:
下一篇: 
文章来自:
文章标签:
网摘收录:
回复
|
|
]