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

2021-08-30 11:23:28 字數 2024 閱讀 9792

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

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

9 10 //3、獲取所有選中的checkbox

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

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

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

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

15 16 //4、獲取checkbox值

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

18 $("#cbcheckbox1").val();

19 20

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

22 var vals = ;

23 $('input:checkbox:checked').each(function (index, item) );

26

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

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

34 });

35 36 //7、設定checkbox為選中狀態

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

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

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

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

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

43 44 //9、設定checkbox為禁用狀態(jquery<1.6用attr,jquery>=1.6建議用prop)

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

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

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

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

49 50 //10、設定checkbox為啟用狀態(jquery<1.6用attr,jquery>=1.6建議用prop)

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

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

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

54 $("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的各種操作

獲取所有的checkbox input type checkbox input name cb 獲取所有選中的checkbox input checkbox checked input type checkbox checked input type checkbox checked input n...