sending http request with ajax each time select box is changed

后端 未结 1 1438
無奈伤痛
無奈伤痛 2021-01-26 07:57

Hi I am using jquery to get data from mysql. It works wonderfully but I need it to update a select box when another select box is changed. Here is what I have for jquery:

<
相关标签:
1条回答
  • 2021-01-26 08:21

    I'm not sure why you are using live - is the original select sometimes replaced via AJAX as well? It may be, however, that you simply need to clear the original values first since you are only appending the new ones.

    $("#airports").live('change',function() 
    {
        var selectVal = $('#airports :selected').val();
        alert(selectVal);
    
        var $output = $('#output').html('');
    
        $.ajax({                                      
          url: 'tras-hotels.php',                         
          data: "n="+selectVal,                                                     
          dataType: 'json',                
          success: function(data)         
          {
             $.each(data, function(key,value) {
                  $output.append("<option value='"+value.id+"'>"+value.hotel+"</option>");
             });
          } 
        });
    
    });
    
    0 讨论(0)
提交回复
热议问题