简体   繁体   English

如何使用依赖下拉列表在ajax中发送数据

[英]How to send data in ajax with dependent drop-down

I Have already set the first value from the first combo-box now i need to send the second variable from second combo-box and receive it in the same php file here is the Ajax:我已经设置了第一个组合框的第一个值,现在我需要从第二个组合框发送第二个变量并在同一个 php 文件中接收它,这里是 Ajax:

$(document).ready(function(){
   $(".rutas").change(function(){
     var id=$(this).val();
     var dataString = 'id='+ id;
     $.ajax({
       type: "POST",
       url: "asientos.php",
       data: dataString,
       cache: false,
       success: function(html){
          $(".asientos").html(html);}  
       }); 
   }); 
});
</script>

Here is the first html combo-box this is functioning perfectly:这是第一个完美运行的 html 组合框:

<select name="rutas" class="rutas" >
   <option value="">Seleccione Ruta</option>;
   <?php   include 'rutas.php';   ?>
</select>

Here is the second html combo-box that i want to get the value from and send it to the same php file as the first:这是我想从中获取值并将其发送到与第一个相同的 php 文件的第二个 html 组合框:

Clase: <select name="clase" class="clase">
   <option value="A">Clase Ejecutiva</option>;
   <option value="B">Clase Media</option>;
   <option value="C">Clase Economico</option>;
</select>

Any help would be appreciated, Thank you!任何帮助将不胜感激,谢谢!

Send multiple values to ajax use object将多个值发送到 ajax 使用object

var data = {field1: "value1", field2: "value2"};

In your case, you should use form在您的情况下,您应该使用form

<form id="myForm">
<select id="select1"></select>
<select id="select2"></select>
</form>

JavaScript code. JavaScript 代码。

$(document).ready(function(){
  $('#myForm').submit(function(event){
     event.preventDefault();
     var data = {field1: $("#select1").val(), field2: $("#select2").val()};

     //Ajax code should be here
     $.ajax({
         type: "POST",
         url: "asientos.php",
         data: data,
         cache: false,
         success: function(html){
            $(".asientos").html(html);}  
         }); 
     }); 
   });
});

Edit : As per the comments编辑:根据评论

function ajaxCall(url, data){

     //Ajax code should be here
     $.ajax({
         type: "POST",
         url: url,
         data: data,
         cache: false,
         success: function(html){
            $(".asientos").html(html);}  
         }); 
     }); 
}

On each select change method you can call this ajax function and populate your drop-down.在每个选择更改方法上,您都可以调用此 ajax 函数并填充下拉列表。

$(".rutas").change(function(){
    ajaxCall('demo.php', '10');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM