欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jquery 實(shí)現(xiàn)checkbox全選,反選,全不選等功能代碼(奇數(shù))

 更新時(shí)間:2012年10月24日 10:55:56   作者:  
jquery 實(shí)現(xiàn)全選,反選,全不選等功能,下面直接以例子進(jìn)行說明,需要的朋友可以參考下
設(shè)頁面有如下一組復(fù)選框和幾個(gè)相關(guān)按鈕(全選,反選,全不選等):
復(fù)制代碼 代碼如下:

<input type="checkbox" name="fruit" value="apple" />蘋果
<input type="checkbox" name="fruit" value="orange" />橘子
<input type="checkbox" name="fruit" value="banana" />香蕉
<input type="checkbox" name="fruit" value="grape" />葡萄
<input type="button" id="btn1" value="全選">
<input type="button" id="btn2" value="全不選">
<input type="button" id="btn3" value="反選">
<input type="button" id="btn4" value="選中所有奇數(shù)">
<input type="button" id="btn5" value="獲得選中的所有值">

則分別實(shí)現(xiàn)相關(guān)功能的完整代碼如下:
復(fù)制代碼 代碼如下:

$(function(){
$('#btn1').click(function(){//全選
$("[name='fruit']").attr('checked','true');
});
$('#btn2').click(function(){//全不選
$("[name='fruit']").removeAttr('checked');
});
$('#btn3').click(function(){//反選
$("[name='fruit']").each(function(){
if($(this).attr('checked')){
$(this).removeAttr('checked');
}else{
$(this).attr('checked','true');
}
})
});
$("#btn4").click(function(){//選中所有奇數(shù)
$("[name='fruit']:even").attr('checked','true');
})
$("#btn5").click(function(){//獲取所有選中的選項(xiàng)的值
var checkVal='';
$("[name='fruit'][checked]").each(function(){
checkVal+=$(this).val()+',';
})
alert(checkVal);
})
});

注意使用 jquery 之前必須要引入 jquery 包哦!

相關(guān)文章

最新評(píng)論