zoukankan      html  css  js  c++  java
  • jQuery对checkbox的各种操作

    jQuery对checkbox的各种操作

    1、根据id获取checkbox

    1 $("#cbCheckbox1");

    2、获取所有的checkbox

    1 $("input[type='checkbox']");//or
    2 $("input[name='cb']");

    3、获取所有选中的checkbox

    1 $("input:checkbox:checked");//or
    2 $("input:[type='checkbox']:checked");//or
    3 $("input[type='checkbox']:checked");//or
    4 $("input:[name='ck']:checked");

    4、获取checkbox值

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

    5、获取多个选中的checkbox值

    1 var vals = [];
    2 $('input:checkbox:checked').each(function (index, item) {
    3     vals.push($(this).val());
    4 });

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

    1 $("#cbCheckbox1").click(function () {
    2     if ($(this).prop("checked")) {
    3         alert("选中");
    4     } else {
    5         alert("没有选中");
    6     }
    7 });

    7、设置checkbox为选中状态

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

    8、设置checkbox为不选中状态

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

    9、设置checkbox为禁用状态(jquery<1.6用attr,jquery>=1.6建议用prop)

    1 $("input[type='checkbox']").attr("disabled", "disabled");//or
    2 $("input[type='checkbox']").attr("disabled", true);//or
    3 $("input[type='checkbox']").prop("disabled", true);//or
    4 $("input[type='checkbox']").prop("disabled", "disabled");

    10、设置checkbox为启用状态(jquery<1.6用attr,jquery>=1.6建议用prop)

    1 $("input[type='checkbox']").removeAttr("disabled");//or
    2 $("input[type='checkbox']").attr("disabled", false);//or
    3 $("input[type='checkbox']").prop("disabled", "");//or
    4 $("input[type='checkbox']").prop("disabled", false);

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

  • 相关阅读:
    Oracle数据库ORA-12154: TNS: 无法解析指定的连接标识符详解
    Oracle Client客户端+PLSQL配置
    函数和存过的创建调用
    MVC学习二:基础语法
    前台和后台的相互传值
    MVC学习一:EF
    博客转发小工具2
    博客转发小工具1
    博客园页面设置
    js问题杂记
  • 原文地址:https://www.cnblogs.com/cyfblogs/p/13639481.html
Copyright © 2011-2022 走看看