jquery如何判断是否选中checkbox


本文摘自PHP中文网,作者coldplay.xixi,侵删。

jquery判断是否选中checkbox的方法:1、使用is判断,代码为【if($('#checkbox-id').is(':checked'))】;2、使用attr判断,代码为【if ($('#checkbox-id').attr()】。

推荐:《jquery视频教程》

本教程操作环境:windows7系统、jquery3.5.1版本,该方法适用于所有品牌电脑。

jquery判断是否选中checkbox的方法:

方法一(建议):

1

2

3

if ($("#checkbox-id").get(0).checked) {

    // do something

}

感谢豆瓣绿补充:

1

2

3

if ($("#checkbox-id")[0].checked) {

    // do something

}

方法二(建议):

1

2

3

if($('#checkbox-id').is(':checked')) {

    // do something

}

方法三(可能无效):

1

2

3

if ($('#checkbox-id').attr('checked')) {

    // do something

}

方法四:

1

2

3

if ($('#checkbox-id').prop('checked')) {

    // do something

}

jquery对checkbox的各种操作

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

//注意: 操作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) {

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

    });

     

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

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

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

            alert("选中");

        } else {

            alert("没有选中");

        }

    });

    //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);

相关免费学习推荐:JavaScript(视频)

以上就是jquery如何判断是否选中checkbox的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

如何判断jQuery class是否存在

jQuery中html()方法怎么用

jQuery如何处理ajax缓存问题

jQuery如何获取鼠标位置

jQuery中find()与children()的区别是什么?

jQuery中怎么修改css样式

jQuery符号===和==区别是什么

jQuery 数组如何判断是否包含某元素

jq如何改变css样式

如何用jQuery判断数据是否加载完成

更多相关阅读请进入《jQuery》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...