使用json和ajax从数据库中检索下拉列表选项时出现问题

时间:2016-02-20 20:25:56

标签: javascript php jquery json ajax

我有一系列的3下拉列表,其中第二次和第三次更改的值取决于之前选择的内容。但目前这些价值观并未出现在移动版本中,而在完整的桌面版本中,一切正常。我有这部分代码。对不起,如果凌乱

// JAVASCRIPT

        function limpiar(combo)
    {
    var combo2 = document.getElementById(combo);
       while(combo2.length > 0)
       {
          combo2.remove(combo2.length-1);
       }
    }
    function llenar(json, combo)
    {
        var combo2 = document.getElementById(combo); 
        combo2.options[0] = new Option('Todos', 'Todos');
        for(var i=0;i<json.length;i++){
            combo2.options[combo2.length] = new Option(json[i].data, json[i].id);
        }
    }
    function cambmar()
    {
        var combo0 = document.getElementById("idmod");
        combo0.options[0] = new Option('Todos', 'Todos');
        var combo1 = document.getElementById("idcat"); 
        var combo2 = document.getElementById("idmar"); //con jquery: $("#"+combo2)[0];
        limpiar(combo2.id);
        limpiar(combo0.id);
        if(combo1.options[combo1.selectedIndex].value != "")
        {
            $.ajax({type: 'get', dataType: 'json', url: 'mar.php', data: {valor: combo1.options[combo1.selectedIndex].value},success: alert("hola");  });
        }
    }

    function cambmod()
    {
        var combo0 = document.getElementById("idcat");
        var combo1 = document.getElementById("idmar"); 
        var combo2 = document.getElementById("idmod"); //con jquery: $("#"+combo2)[0];
        limpiar(combo2.id);
        if(combo1.options[combo1.selectedIndex].value != "")
        {
        $.ajax({type: 'get', dataType: 'json', url: 'mod.php', data: {valor: combo0.options[combo0.selectedIndex].value, mar: combo1.options[combo1.selectedIndex].value}, success: function(json){ llenar(json, combo2.id);}});
    }
}

// HTML

    <div  class="container-fluid">
            <div  style="text-align: center;"  class="boxCategoria">Categoria:</div>            
                <select name="cat" class="categoria" id="idcat" onchange="cambmar()">
                        <option value="0">Todos</option>
                        <option value="1">Automóviles</option>
                        <option value="2">Camionetas</option>
                        <option value="3">Motocicletas</option>
                    </select>
   </div>

              <div  class="container-fluid">
                <div  style="text-align: center;"  class="boxMarca">Marca:</div>

                    <select  name="mar"  class="marca"  id="idmar"  onchange="cambmod()">
                        <option  value="Todos">Todos</option>
                    </select>

                 </div>

                <div  class="container-fluid">
                <div  style="text-align: center;"  class="boxModelo">Modelo:</div>

                    <select name="mod" class="modelo" id="idmod">
                        <option value="Todos">Todos</option> 
                    </select>
                </div>

// mar.php

<?php
   $valor = $_GET['valor'];
   include("conexion.php");

   $CmdSQL="SELECT DISTINCT mar FROM autos WHERE cat=$valor";
   $runquery=mysql_query ($CmdSQL,$conecta);
   if (mysql_num_rows($runquery)!=0)
   {
      if (mysql_num_rows($runquery)!=0)
      {
         while($marc=mysql_fetch_row($runquery))
         {
            $_arreglo[] = array('id' => $marc[0], 'data' => $marc[0]);  
         }
      }
      mysql_close();
      echo json_encode($_arreglo);
   }
?>

0 个答案:

没有答案