Limit number of selectable options in a multiple select

0

I have this multiple select:

<select multiple="" class="form-control" name="evaluador[]" id="evaluador">
<option value="1">Casa</option>
<option value="2">Apartamento</option>
<option value="2">Finca</option>                           
</select>

What I need is that it only allows me to select two of the options that are shown to send it to a database.

    
asked by Hector Ariza 31.10.2017 в 18:18
source

4 answers

1

You can do it with jQuery:

$("select#evaluador").change(function () {
  if($("select#evaluador option:selected").length > 2) {
    //tu código aquí
   }
});

This will limit the options to 2 to 2.

    
answered by 31.10.2017 / 18:44
source
1

Something you can do is create a JavaScript controller that keeps an account of the last valid option and if you choose an invalid option (eg choose more than 2 values), then return to the last valid option.

Something like this:

var ultimoValorValido = null;
$("#evaluador").on("change", function() {
  if ($("#evaluador option:checked").length > 2) {
    $("#evaluador").val(ultimoValorValido);
  } else {
    ultimoValorValido = $("#evaluador").val();
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<select multiple="" class="form-control" name="evaluador[]" id="evaluador">
  <option value="1">Casa</option>
  <option value="2">Apartamaento</option>
  <option value="3">Finca</option>
  <option value="4">Finca 2</option>    
</select>
    
answered by 31.10.2017 в 18:58
0

Hello, this worked for me.

    $("#evaluador").on('change', function(e) {
        if (Object.keys($(this).val()).length > 2) {
            $('option[value="' + $(this).val().toString().split(',')[2] + '"]').prop('selected', false);
        }
    });

What it does is that at the change event of the select we are going to allow only 2 options, when the 3rd is marked, it is immediately unchecked, asicional to this, a message with the warning could also be launched.

    
answered by 31.10.2017 в 19:47
-1

you bring the information that you are going to put into the database select

example here the logical model

public function listar(){
        $datos = array();
        $consulta="SELECT * FROM tblProductos p inner join tblSubcategorias s on p.idSubcategoria=s.idSubcategoria inner join tblCategorias c on c.idCategoria=s.idCategoria";
        $resultado = $this->conexion->query($consulta);
        while ($filaTmp = $resultado->fetch_assoc()) {
            $datos [] = $filaTmp;
        }
        if($resultado){
            return $datos;
        }
        elseif (!$this->conexion->query($consulta)) {
            echo $this->conexion->errno . " : " . $this->conexion->error . "\n";
        }
    }  

driver send the data to the view

require_once("../../model/productos.php");
                $varProductos= new productos();
                $productos = $varProductos->listar();

you do the route in the option

<div class="form-group col-md-12">
          <label class="control-label">Productos</label>
          <select class="form-control input-sm selectpicker" name="producto[]" multiple data-error="Es un campo obligatorio" data-live-search="true" required="required">
          <?php 
          foreach ($productos $producto){                              
          echo '<option value="'.$producto['idProducto'].'">'.$producto['producto'].'</option>';
          ?>
          <?php
          }
          ?>
          </select>
          <div class="help-block with-errors">&nbsp;</div>
        </div>
    
answered by 31.10.2017 в 18:26