Limit jQuery checkbox

Asked

Viewed 910 times

0

I am creating a system where I list images of the bank and below each image has a checkbox, all this within a for to take the images of the bank, wanted a function to be able to limit the amount of checkbox or photos that the user can select, I’m using CI, 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’ll also leave the scren for you to follow scren da tela

1 answer

2


Well, you can count the number of checkboxes marked using jQuery and then compare with the set limit 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: https://jsfiddle.net/0mj39ksx/2/

  • I used the function and set the limit to 1 but it didn’t happen, I played the code in the own view for testing and it should work

  • Hello. Unfortunately I posted the error code the first time, test again to see what happens. I also put an example in Jsfiddle.

  • The code was right I just gave a modification and it worked.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.