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 を 8 ではなく「X5」として保存する必要があります。

どうすればこれを改善できますか?

4

2 に答える 2

1

さて、( ) のi2 番目の引数として渡しているので、カウンターの値がオプションの値になります。実際の配列要素を値にしたい場合は、代わりに渡します。Optionnew Option(json[car_make][i], i)

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

または、テキストと値が同じであるため、単純に 2 番目の引数を省略します。

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>

問題は、オプション タグの値属性に整数がある場合、テキストの内容に関係なく、その整数を取得することです。たとえば、ここでモデル 2 を選択した場合、フォームの値は 2 のままです。

それはすべて、これらのドロップダウンをどのように作成しているかにかかっています!

于 2013-07-18T00:16:19.387 に答える