user3782635 user3782635 - 7 months ago 10
Javascript Question

display a select list depending on other select

i am trying to show a second select depending on what i choose on the first select !! here is wjat i proceed

$("select#semestre").change(function(){
var sem = $(this).val();
var data = {"sem":sem};
$.post('myScript.php',data,function(result){
$("select#matiere").append(result);
});
});


my code work fine at all but when i change a select option it add another options to the old one , what i want is to remove the old one and display another result

Answer

when you choose another select option you clean the select and append to it the result which coming by the request !! not sure but i'm suggestion you to try this

$("select#semestre").change(function(){
   var sem = $(this).val();
  var data  = {"sem":sem};
  $.post('myScript.php',data,function(result){
     //here the select will be empty and then it will be fulled with the result coming from your post
     $("select#matiere").html("");
     $("select#matiere").append(result);
  });
});
Comments