2016-06-09 40 views
0

我正在根据用户数创建复选框(复选框数组)。 如果没有用户比复选框不会创建。检查并计算选中的复选框数

问题: 我要检查,如果复选框(ES)是/是存在或不和 如果复选框(ES)存在比我要算被选中的复选框。

代码:

<?php 
    if (isset($companyusers) && $companyusers != array()) { 
     foreach ($companyusers as $key => $value) { 
      ?> 
      <div class="div_to_hide"> 
       <label class="checkbox" style="display: inline-block !important"> 
        <input name="noti[<?php echo $value->id; ?>]" id="" type="checkbox" class="checkbox_input" value="<?= $value->first_name ?>"> 
         <span class="search_text"> 
          <?= $value->first_name ?> 
         </span> 
       </label> 
      </div> 
      <?php 
     } 
    } 
?> 
<div class="form-group"> 
    <div class="col-lg-offset-2 col-lg-10"> 
     <button type="submit" class="btn smbtn btn-success"> 
      <?php echo Yii::t('app', 'Send'); ?> 
     </button> 
    </div> 
</div> 
+0

的http:// ST ackoverflow.com/questions/4796036/how-do-i-count-how-many-checkboxes-are-selected-on-a-page-with-jquery –

回答

1

尝试:

演示:https://jsfiddle.net/r00xxgk5/

if($('input:checkbox[name="noti[]"]').length > 0) { // check length of checkbox if > 0 
    var sList = ""; 
    $('input:checkbox[name="noti[]"]').each(function() { // loop throgh all checkbox and detect if checked or not 
     sList += "(" + $(this).val() + "-" + (this.checked ? "checked" : "not checked") + ")"; 
    }); 
    console.log (sList); 
} 
0

怎么样使用count:

count($noti);