如果可见,请选中复选框

Moh*_*ain 1 checkbox jquery

我有一个复选框列表,我有一个select_all复选框.请检查代码中的注释.

$('#select_all').change(function() {        
    var checkboxes = $("input[name^='select']");
    if($('#select_all').is(':checked')) {
           //here i want to check where this checkbox (checkbox from the list not select_all checkbox) is visible or not.
           // if visible then check the checkbox
        checkboxes.attr('checked', 'checked');
    } else {
        checkboxes.removeAttr('checked');
    }
});
Run Code Online (Sandbox Code Playgroud)

是否有任何想法来检查能见度: -

  $("input[name^='select'][checked]").each(   
         function() {   
                // Insert code here   
             }   
  );
Run Code Online (Sandbox Code Playgroud)

Mat*_*att 7

使用:visible选择器.

$('#select_all').change(function() {        
    var checkboxes = $("input[name^='select']");

    if (this.checked) {
        checkboxes.filter(':visible').attr('checked', true);
    } else {
        checkboxes.attr('checked', false);
    }
});
Run Code Online (Sandbox Code Playgroud)

注意我是如何使用正确的设置checked属性的方法的; 值应该是布尔值,而不是字符串.