代码之家  ›  专栏  ›  技术社区  ›  Richard Knop

是否选中复选框(在每个()循环内)?

  •  0
  • Richard Knop  · 技术社区  · 15 年前

    我正在使用同一个类和函数each()循环多个复选框,在循环中,我需要能够判断复选框是否被选中。怎么做?

    $('.checkbox').each(function() {
        var is_checked = 'no';
        // I need to change the value of the is_checked variable to 'yes'
        // if the checkbox is checked
        //
    });
    
    6 回复  |  直到 15 年前
        1
  •  5
  •   BalusC    15 年前

    使用

    var is_checked = this.checked ? 'yes' : 'no';
    
        2
  •  4
  •   Jeremy Morgan    15 年前

    $('#yourcheckbox').is(':checked'); 
    
        3
  •  3
  •   Agent_9191    15 年前

    为什么不使用 :checkbox:checked 取而代之的是选择器?这样,您将获得所有选中复选框的列表,并对这些复选框进行迭代。

        4
  •  2
  •   Chad    15 年前
    $("input:checkbox[class='.checkbox']:checked").each(function () {
     // this will only loop thru checked boxes (checked = true)
    }
    

    $('.checkbox').each(function() {    
     // or return a boolean for each one
     var is_checked = $(this).is(":checked");
    });
    
        5
  •  1
  •   bytebender    15 年前

    $(this).val() 如果选中,则会给您“true”,然后 $(this).val("true") 将设置值

        6
  •  1
  •   Harry dbr    11 年前

    这将获得所有未选中的框

    $("input:not(:checked)")