两个依赖的下拉列表php数组

时间:2017-02-02 00:43:18

标签: javascript php arrays dropdown

我正在尝试替换使用源代码中可见的javascript和数组的代码。

我希望能够使用php数组创建它,或者使用AJAX并将其存储在另一个文件中。我不知道如何制作正确的php命令或数组

var cars= new Array();

cars["OTHER"] = new Array("Heavy Machinery","Semi-Truck","Pickup Truck","Sedan","SUV","Misc");
cars["ATV"] = new Array("small","large");
cars["Boat"] = new Array("Under 20 Feet","Over 20 Feet");
cars["Motorcycle"] = new Array("250CC","500CC","700CC","900+");
cars["RV"] = new Array("Under 25 Feet","Over 25 Feet","5th Wheel");
cars["AC"] = new Array("Cobra");
cars["Acura"] = new Array("1.6 EL","1.7 EL","2.3 CL","2.5 TL","3.0 CL","3.2 TL","3.5 RL","CL","CSX","EL","ILX","Integra","Legend","MDX","NSX","NSX-T","RDX","RL","RSX","SLX","TL","TSX","Vigor","ZDX");
cars["Alfa Romeo"] = new Array("145","146","147","155","156","159","164","166","33","75","308","1900","2600","4C","6C","8C","Alfasud","Alfetta","Berlina","Bimotore","Canguro","Corsa","Disco Volante","Duetto","G1","GT","GTV","GTV-6","GTV6","Giulia","Guiletta","GP","Grand Prix","GTA","Iguana","Junior Z","Milano","Montreal","Navajo","P1","P2","P3","Quadrifoglio","RL","RM","Scarabeo","Spider","Sports Car","Sportwagon","Stradle","Tipo","Torpedo");
........................ALL OTHER MAKES AND MODELS ARE IN BETWEEN........................
cars["Yugo"] = new Array("55","Cabrio","GV");

jQuery(document).ready(function($){

$('span.text select').change(function(){
$(this).siblings('.value').text($(this).find('option[value="'+$(this).val()+'"]').text());
});

for ( make in cars )
{
$('#formmake').append('<option value="'+make+'">'+make+'</option>');
}

$('#formmake').change(function(){
var val = $(this).val();
$('#formmodel').html('<option value="">Select Model</option>');
for ( i in cars[val] )
{
$('#formmodel').append('<option value="'+cars[val][i]+'">'+cars[val][i]+'</option>');
}
$('#formmodel').append('<option value="Other">- Other -</option>');
});

$('#formmake, span.text select').each(function(){
var def = $(this).siblings('.value').text();
$(this).find('option[value='+def+']').attr('selected', 'selected');
$(this).change();
});



});

----------------------这就是我想要隐藏网站上的来源----------- --------- 这是我的PHP获取MAKE但是如何创建模型数组,以便当用户选择汽车时,相应的MODELS将​​填充在相​​应的下拉列表中(称为Select Model:)

<?php
    $car_make = array('ATV','Boat','Motorcycle','Acura','Alfa Romeo','AM              General');  //this is only a partial array, it will have all the makes

    echo '<select name="car_make">';
    for($i = 0; $i < count($car_make);$i++)
    {
        echo '<option value="'. ($i + 1) . '">' . $car_make[$i] . '</option>';
    }
    echo '</select>';

?>

如何使用车辆模型创建第二个数组,该模型将使用第一个数组的选项值来查找make,然后从模型数组中提取相应的make?

1 个答案:

答案 0 :(得分:0)

您可以使用jQuery选择第一个选择菜单的值,然后使用它与make的特定值进行比较,然后显示其特定的模型

&#13;
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <td>Hour</td>
    <td>Day</td>
    <td>None</td>
    <td>hour and day</td>
    <td>Hour and Day</td>
  </tr>
</table>
&#13;
var model=new Array();
function getmodel() {
var make=$('select[name=make]').val();
if(make==='toyota'){$("select[name=model]").html('');$("select[name=model]").append("<option value='corolla'>Corolla</option><option value='camry'>Camry</option><option value='hilux'>Hilux</option>");}

if(make==='honda'){$("select[name=model]").html('');$("select[name=model]").append("<option value='civic'>Civic</option><option value='jazz'>Jazz</option><option value='accord'>Accord</option>");}

if(make==='suzuki'){$("select[name=model]").html('');$("select[name=model]").append("<option value='cultus'>Cultus</option><option value='vitara'>Vitara</option>");}

if(make==='bugatti'){$("select[name=model]").html('');$("select[name=model]").append("<option value='veyron'>Veyron</option><option value='chiron'>Chiron</option>");}  
}
&#13;
&#13;
&#13;