jQuery對checkbox的各種操作

2022-05-15 06:42:58 字數 1644 閱讀 6997

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

//1、根據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) );

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

$("#cbcheckbox1").click(function () else

});//7、設定checkbox為選中狀態

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

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

//8、設定checkbox為不選中狀態

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

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

//9、設定checkbox為禁用狀態(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、設定checkbox為啟用狀態(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);

jquery對checkbox的操作

一 通過選擇器選取checkbox 1.給checkbox設定乙個id屬性,通過id選擇器選取 inputtype checkbox name mybox id chkone value 1 checked checked jquery chkone click function 2.給checkb...

jquery中對CheckBox的操作

1.獲取所有name notify 的選中的checkbox的值 var notifyary var textary input checkbox name notify checked each function jquery判斷checked的三種方法 attr checked 看版本1.6 返...

jQuery對checkbox的各種操作(詳細)

1 注意 操作checkbox的checked,disabled屬性時jquery1.6以前版本用attr,1.6以上 包含 建議用prop 2 3 1 根據id獲取checkbox 4 cbcheckbox1 5 6 2 獲取所有的checkbox 7 input type checkbox or...