0

我使用了这段代码并创建了数组:

    <script type="text/javascript">
  var json = {
    "Merk": ["Model - Kies eerst een merk"],
    "Abarth": ["500","500C","Punto"],
    "Alfa Romeo": ["159 Sportwagon","Giulietta","MiTo",],
    "Aston Martin": ["Cygnet","DB9 Coupe","DB9 Volante","Rapide","V12Vantage Coupe","V12Vantage Roadster","V8Vantage Coupe","V8Vantage Roadster","Vanquish"],
    "Audi": ["A1","A3","A4","A5","A6","A7","A8","Q3","Q5","Q7","R8","RS4","RS5","S4","S5","S6","S7","S8","SQ5","TT"],
    "BMW": ["1 Serie","3 Serie","5 Serie","6 Serie","7 Serie","X1","X3","X5","X6","Z4"],
    "Bentley": ["Continental GT","Continental GTC","Flying Spur"],
    "Chevrolet": ["Aveo","Camaro","Captiva","Corvette","Cruze","Orlando","Spark","Trax","Volt"],
    "Citroën": ["Berlingo","C-Zero","C1","C3","C3 Picasso","C4","C4 Aircross","C4 Picasso","C5 Sedan","C5 Tourer","C6","C8","DS3","DS3 Cabrio","DS4","DS5","Grand C4 Picasso","Nemo",],"Dacia": ["Duster","Lodgy","Sandero","SanderoStepway"],
    "Ferrari": ["458","California","F12","FF"],
    "Fiat": ["500","Bravo","Doblo","Panda","Punto","Qubo","Sedici"],
    "Fisker": ["Karma"],
    "Ford": ["B-Max","C-Max","Fiesta","Focus","Galaxy","Grand C","KA","Kuga","Mondeo","S-Max"],
    "Honda": ["Accord","Accord Tourer","CR-V","CR-Z","Civic","Insight","Jazz","Jazz Hybrid"],
    "Hyundai": ["Genesis","Santa Fe","Veloster","i10","i20","i30","i40","ix20","ix35"],
    "Infiniti": ["EX","FX","G","M"],
    "Jaguar": ["F-Type","XF","XF Sportbrake","XJ Series","XK","XKR"],
    "Jeep": ["Compass","Grand Cherokee","Wrangler","Wrangler Unlimited"],
    "Kia": ["Carens","Cee'd","Cee'd Sportswagon","Optima","Picanto","Pro Cee´d","Rio","Sorento","Soul","Sportage","Venga"],
    "Lamborghini": ["Aventador","Gallardo"],
    "Lancia": ["Delta","Flavia","Thema","Voyager","Ypsilon"],
    "Land Rover": ["Defender","Discovery","Freelander","Range Rover","Range Rover Evoque","Range Rover Sport"],
    "Lexus": ["CT","GS","IS","IS250C","LS 600h","RX"],
    "Lotus": ["Elise","Evora","Exige"],
    "Maserati": ["GranCabrio","GranTurismo","Quattroporte"],
    "Maybach": ["Maybach"],
    "Mazda": ["CX-5","MX-5","Mazda2","Mazda3","Mazda5","Mazda6","Mazda6 Sportbreak"],
    "Mercedes Benz": ["A-Klasse","B-Klasse","C-Klasse","CL-Klasse","CLA-Klasse","CLS-Klasse","E-Klasse","G-Klasse","GL-Klasse","GLK-Klasse","M-Klasse","S-Klasse","SL-Klasse","SLK-Klasse","SLS AMG","Viano"],
    "Mini": ["Cabrio","Clubman","Countryman","Coupé","Mini","Paceman","Roadster"],
    "Mitsubishi": ["ASX","ColtCZ5","Lancer Sportback","Outlander","Pajero","SpaceStar","i-MiEV"],
    "Nissan": ["370Z","EVALIA","GT-R","Juke","Leaf","Micra","Murano","Note","Pathfinder","Pixo","Qashqai","Qashqai + 2","X-Trail"],
    "Opel": ["ADAM","Agila","Ampera","Astra","Cascada","Combo","Corsa","Insignia","Meriva","Mokka","Zafira"],
    "Peugeot": ["107","2008","207","208","3008","308","5008","508","807","Bipper Tepee","Partner Tepee","RCZ","iOn"],
    "Porsche": ["911","911 Cabriolet","Boxster","Cayenne","Cayman","Panamera"],
    "Renault": ["Clio","Clio Estate","Fluence","Grand Espace","Grand Kangoo Family","Grand Scénic","Kangoo Family","Laguna","Laguna Estate","Megane","Megane Coupé","Megane Estate","Scénic","Twingo","ZOE"],
    "Seat": ["Alhambra","Altea","Altea Freetrack","Altea XL","Exeo","Ibiza","Leon","Mii","Toledo"],
    "Skoda": ["Citigo","Fabia","Fabia Combi","Octavia","Octavia Combi","Rapid","Roomster","Superb","Superb Combi","Yeti"],
    "Smart": ["Fortwo cabrio","Fortwo coupe"],
    "SsangYong": ["Actyon Sports","Korando","Rexton"],
    "Subaru": ["BRZ","Forester","Legacy Outback","Legacy Touring Wagon","Trezia","WRX STI","XV"],
    "Suzuki": ["Alto","Grand Vitara","Jimny","Kizashi","SX4","Splash","Swift"],
    "Tesla": ["Model S"],
    "Toyota": ["Auris","Auris Touring Sports","Avensis Sedan","Avensis Wagon","Aygo","GT86","IQ","Landcruiser","Landcruiser V8","Prius","Prius Wagon","RAV4","Verso","Verso-S","Yaris"],
    "Volkswagen": ["Beetle","CC","Caddy","Golf","Jetta","Multivan","Passat","Phaeton","Polo","Sharan","Tiguan","Touareg","Touran","up!"],
    "Volvo": ["C70","S60","S80","V40","V40 Cross Country","V60","V70","XC60","XC70","XC90"]
  };

  function carMake () {
    select = document.getElementById('dropdown1');
    select.options.length = 0;
    for(make in json) {
        select.options[select.options.length] = new Option(make, make);
    }
}

function carModel(sel) {
    var car_make = sel.options[sel.selectedIndex].value
    select = document.getElementById('dropdown2');
    select.options.length = 0;
    for(var i=0;i<json[car_make].length;i++) {
        select.options[select.options.length] = new Option(json[car_make][i], i);
    }
  }
</script>

它填充了我的下拉菜单,选择品牌将为我提供可供选择的模型。该表单返回汽车品牌的全名字符串。但是对于模型,它返回一个整数。对应于数组中的位置。

我需要将 BMW 存储为汽车制造商,并将 X5 存储为“X5”而不是 8

我该如何改进呢?

4

2 回答 2

1

好吧,您将i作为第二个参数传递给Option( new Option(json[car_make][i], i)),因此计数器的值成为选项的值。如果您希望实际的数组元素成为值,请改为传递它:

new Option(json[car_make][i], json[car_make][i]);

或者由于文本和值相同,只需省略第二个参数:

new Option(json[car_make][i]);

(同样适用new Option(make, make),你可以使用new Option(make)

如果一个option元素没有明确地具有值,则其内部文本将成为该值。

于 2013-07-18T00:17:45.937 回答
0

你如何填充你的下拉列表?我想它可能看起来像这样:

<select>
    <option value="1">Model 1<option>
    <option value="2">Model 2<option>
<select>

问题是,如果选项标签的 value 属性中有一个整数,那么无论文本说什么,你都会得到那个整数。所以举个例子,这里如果我选择了Model 2,表格的值还是2。

这一切都取决于您如何创建这些下拉菜单!

于 2013-07-18T00:16:19.387 回答