0

I have a select option which is working perfectly but only one time, the request does not executes again.

I think it's because of the 'onchange' event.

This is my ajax code :

jQuery(document).ready(function($) {
$('#referenceProduit').change(function(){
    //on recupere la valeur de l'attribut value pour afficher tel ou tel resultat
    var req=$('#referenceProduit').val();
    //requête ajax, appel du fichier function.php
    $.ajax({
        type: "POST",
        url: "include/php.php",
        data: "referenceProduit="+req,
        dataType : "json",
        //affichage de l'erreur en cas de problème
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest + '--' + textStatus + '--' + errorThrown);
        },
        //function s'il n'y a pas de probleme
        success:function(data){
            //On affiche la réponse du serveur
            $('.produit').empty();
            $('.produit').prepend(data.produit);
            $('input[name="designation"]').val(data.resDisplayForm.designation);
            $('input[name="prix"]').val(data.resDisplayForm.prix);
        }
    });
});
}); 

HTML code :

    <div class="form-group">
  <label for="referenceProduit" class="col-sm-1 control-label">Reference</label>
  <div class="col-sm-2">
    <select class="form-control" name="referenceProduit" id="referenceProduit">
      <option selected="selected" disabled="disabled">Choisir</option>
      <?php foreach($lesProduits as $unProduit){?>
      <option name="<?php echo $unProduit['id'];?>" value="<?php echo $unProduit['id'];?>"><?php echo $unProduit['reference']?></option>
      <?php } ?>
    </select>
  </div>
</div>
<div class="form-group">
  <div class="produit"></div>
</div><br/><br/>
Lucas Frugier
  • 277
  • 1
  • 4
  • 12

1 Answers1

1

You should map to

.on(eventType, selector, function)

example

$('#parentElement').on('change', '#referenceProduit', function)
Dhara
  • 1,431
  • 4
  • 29
  • 47