Limit jQuery checkbox

0

I'm creating a system where the bank's ready images and below each image has a checkbox, all this inside a for to get the images of the bank, wanted a function to be able to limit the amount of checkbox ie photos that the user can select, I'm using the IC, follow the view

        <div class="tab-content">
                <div class="tab-pane active" id="select">
                    <h4 class="info-text"> Selecione suas <b>10</b> fotos: </h4>
                    <div class="row">
                    <?php foreach ($for_fotos as $dados) : ?>
                      <div class="col-sm-6 col-md-4">
                        <div class="thumbnail">
                          <img src="<?php echo base_url($dados['path'].'/'.$dados['nome']); ?>" alt="<?php echo $dados['nome'] ?>">
                          <div class="caption">
                            <p>
                              <div class="checkbox">
                                <label>
                                  <input class="limited" type="checkbox" value="">
                                  Selecionar
                                </label>
                              </div>
                            </p>
                          </div>
                        </div>
                      </div>  
                    <?php endforeach; ?>  
                    </div>
                </div>
            </div>
            <div class="wizard-footer">
                    <div class="pull-right">
                        <input type='submit' class='btn btn-next btn-fill btn-warning btn-wd btn-sm' value='PRÓXIMO' />
                        <!--<input type='button' class='btn btn-finish btn-fill btn-warning btn-wd btn-sm' name='finish' value='Finish' />-->
                    </div>
                    <div class="pull-left">
                        <?php echo anchor('selecao/voltar', 'Voltar', 'class="btn btn-previous btn-fill btn-default btn-wd btn-sm"'); ?>
                    </div>
                    <div class="clearfix"></div>
            </div>
            <?php echo form_close(); ?> 
    </div>

I will also leave the scren for you to follow

    
asked by anonymous 30.04.2015 / 15:45

1 answer

2

Well, you can count the number of checked checkboxes using jQuery and then compare with the set threshold value and block the user action as follows.

$(document).on('click', '.limited', function(){
   var limit = 2;
   var counter = $('.limited:checked').length;
   if(counter > limit) {
      this.checked = false;
      alert('Limite atingido');
   }
});

Example: link

    
30.04.2015 / 15:55