JQuery對checkbox的各種操作

//注意: 操作checkbox的checked,disabled屬性時jquery1.6以前版本用attr,1.6以上(包含)建議用prop

? ? //1、根據(jù)id獲取checkbox

? ? $("#cbCheckbox1");

? ? //2、獲取所有的checkbox

? ? $("input[type='checkbox']");//or

? ? $("input[name='cb']");

? ? //3、獲取所有選中的checkbox

? ? $("input:checkbox:checked");//or

? ? $("input:[type='checkbox']:checked");//or

? ? $("input[type='checkbox']:checked");//or

? ? $("input:[name='ck']:checked");

? ? //4、獲取checkbox值

? ? //用.val()即可,比如:

? ? $("#cbCheckbox1").val();

? ? //5、獲取多個選中的checkbox值

? ? var vals = [];

? ? $('input:checkbox:checked').each(function (index, item) {

? ? ? ? vals.push($(this).val());

? ? });


? ? //6、判斷checkbox是否選中(jquery 1.6以前版本 用? $(this).attr("checked"))

? ? $("#cbCheckbox1").click(function () {

? ? ? ? if ($(this).prop("checked")) {

? ? ? ? ? ? alert("選中");

? ? ? ? } else {

? ? ? ? ? ? alert("沒有選中");

? ? ? ? }

? ? });

? ? //7、設(shè)置checkbox為選中狀態(tài)

? ? $('input:checkbox').attr("checked", 'checked');//or

? ? $('input:checkbox').attr("checked", true);

? ? //8、設(shè)置checkbox為不選中狀態(tài)

? ? $('input:checkbox').attr("checked", '');//or

? ? $('input:checkbox').attr("checked", false);

? ? //9、設(shè)置checkbox為禁用狀態(tài)(jquery<1.6用attr,jquery>=1.6建議用prop)

? ? $("input[type='checkbox']").attr("disabled", "disabled");//or

? ? $("input[type='checkbox']").attr("disabled", true);//or

? ? $("input[type='checkbox']").prop("disabled", true);//or

? ? $("input[type='checkbox']").prop("disabled", "disabled");

? ? //10、設(shè)置checkbox為啟用狀態(tài)(jquery<1.6用attr,jquery>=1.6建議用prop)

? ? $("input[type='checkbox']").removeAttr("disabled");//or

? ? $("input[type='checkbox']").attr("disabled", false);//or

? ? $("input[type='checkbox']").prop("disabled", "");//or

? ? $("input[type='checkbox']").prop("disabled", false);

??? 代碼如下

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

? ? <title></title>

</head>

<body>

? ? <h3>jQuery操作checkbox

? ? </h3>

? ? <input type="checkbox" id="cbCheckbox1" value="1" />

? ? <input type="checkbox" value="2" />

? ? <input type="checkbox" disabled="disabled" value="3" />

? ? <input type="checkbox" value="4" />

? ? <input type="checkbox" disabled="true" value="5" />

? ? <br />

? ? <input type="button" id="btnDisabled" value="禁用" onclick="fn_disabled();" />

? ? <input type="button" id="Button1" value="啟用" onclick="fn_enable();" /><br />

? ? <input type="button" id="Button2" value="獲取選中的值" onclick="getCheckedValues();" /><br />

? ? <input type="button" id="Button3" value="選中第二個" onclick="checkedSecond();" />

? ? <input type="button" id="Button4" value="取消選中第二個" onclick="uncheckedSecond();" /><br />

</body>

</html>

<script src="js/jquery-1.9.1.min.js" type="text/javascript"></script>

<script type="text/javascript">

? ? function fn_disabled() {

? ? ? ? //$("input[type='checkbox']").attr("disabled", "disabled");

? ? ? ? //$("input[type='checkbox']").attr("disabled", true);

? ? ? ? $("input[type='checkbox']").prop("disabled", true);

? ? ? ? //? $("input[type='checkbox']").prop("disabled", "disabled");

? ? }

? ? function fn_enable() {

? ? ? ? //? $("input[type='checkbox']").removeAttr("disabled");

? ? ? ? // $("input[type='checkbox']").attr("disabled", false);

? ? ? ? // $("input[type='checkbox']").prop("disabled","");

? ? ? ? $("input[type='checkbox']").prop("disabled", false);

? ? }

? ? //獲取選中的 checkbox的值

? ? function getCheckedValues() {

? ? ? ? var arr = [];

? ? ? ? $("input[type='checkbox']:checked").each(function (index, item) {//

? ? ? ? ? ? arr.push($(this).val());

? ? ? ? });

? ? ? ? alert(arr);

? ? }

? ? function checkedSecond() {

? ? ? ? // $("input[type='checkbox']:eq(1)").prop("checked", "checked");

? ? ? ? $("input[type='checkbox']:eq(1)").prop("checked", true);

? ? }

? ? function uncheckedSecond() {

? ? ? ? //? $("input[type='checkbox']:eq(1)").prop("checked", "");

? ? ? ? $("input[type='checkbox']:eq(1)").prop("checked", false);

? ? }

? ? $("#cbCheckbox1").click(function () {

? ? ? ? if ($(this).prop("checked")) {//jquery 1.6以前版本 用? $(this).attr("checked")

? ? ? ? ? ? alert("選中");

? ? ? ? } else {

? ? ? ? ? ? alert("沒有選中");

? ? ? ? }

? ? });

</script>

jQuery賦值checked的幾種寫法:

所有的jQuery版本都可以這樣賦值,不建議用attr():

$("#cb1").attr("checked","checked"); //通用做法,現(xiàn)在不推薦了

$("#cb1").attr("checked",true); //不標(biāo)準(zhǔn),不推薦了

$("#cb1").attr("checked","true"); //不標(biāo)準(zhǔn),不推薦了

jQuery的prop()的4種賦值(推薦如下寫法):

$("#cb1").prop("checked",true); //標(biāo)準(zhǔn)寫法,推薦!

$("#cb1").prop({checked:true}); //map鍵值對? ?

$("#cb1").prop("checked",function(){

? ? return true;//函數(shù)返回true或false

});

//$("#cb1").prop("checked","checked"); //不標(biāo)準(zhǔn)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容