How to create a filter without repeating the Selected Values [closed]

0

Index.php :

<body>
    <div id="campo"></div>
    <div id="categoria">
        <?php include('selectPaisJquery.php');?>
    </div>
</body>

selectPaisJquery.php

<?php 
    include('configuracion/conexion.php'); 
?>

<script>
  $('document').ready(function() {

  <?php 
    $consulta = "SELECT idPais FROM pais";
    $resultado = $conexion->query($consulta)or die("Error de busqueda o conexion");

    while ($paisBuscado = $resultado->fetch_assoc() ) {
       $pais = utf8_encode($paisBuscado['idPais']);
  ?>
       var paisJq= "<?php echo $pais ?>";
       $('#categoria').append('<option value="'+ paisJq +'" >' + paisJq +'</option>');
  <?php  
    }

    mysqli_free_result($resultado);
    $conexion->close();
  ?>

    $('option').click(function(){
      var valor = $(this).text();
         $('#campo').append(valor);
    });

  });
</script>
    
asked by Gamez 09.03.2017 в 16:14
source

1 answer

0

If you do not want the values to be repeated, you can check if they have already been added in the div using the indexOf like this:

$('option').click(function(){
    var valor = $(this).text();

    if($('#campo').text().indexOf(valor) == -1)
        $('#campo').append(valor);
});
    
answered by 09.03.2017 / 16:48
source