The situation is this: there are 2 selekta, in the first type of machine, in the second model of the machine. The values ​​in the second select change depending on the selection in the first select. When choosing in the second selection, the picture of the car model should change, but I cannot figure out how to do it.

Here is the code:

var mkLists = new Array(4) mkLists["empty"] = ["Π’Ρ‹Π±Π΅Ρ€ΠΈΡ‚Π΅ ΠΌΠ°Ρ€ΠΊΡƒ ΠΌΠ°ΡˆΠΈΠ½Ρ‹"]; mkLists["passenger"] = ["Hyundai", "Toyota Camry 40", "KIA Optima"]; mkLists["BusinessClass"] = ["Toyota Land Cruiser", "Mercedes-Benz S 350-l", "Mercedes-Benz S 500-l", "KIA Cadenza", "Ssangyong Chairman Limo"]; mkLists["Multiplace"] = ["Mercedes Benz Tourismo", "Hyundai Aero City", "Hyundai Universe Luxury", "Hyundai Counti", "Toyota Coaster", "Toyota hiace"]; mkLists["SpecMachinery"]= ["Π“Π°Π· 330232", "Π“Π°Π· 330202", "Камаз 45143", "ΠŸΡ€ΠΈΡ†Π΅ΠΏ (Камаз 45143)"]; function mkChange(selectObj) { var id = selectObj.selectedIndex; var which = selectObj.options[id].value; carsList = mkLists[which]; var carSelect = document.getElementById("brand"); while (carSelect.options.length > 0) { carSelect.remove(0); } var newOption; for (var i=0; i<carsList.length; i++) { newOption = document.createElement("option"); newOption.value = carsList[i]; newOption.text=carsList[i]; try { carSelect.add(newOption); } catch (e) { carSelect.appendChild(newOption); } } //ΠΊΠΎΠ½Π΅Ρ† Ρ†ΠΈΠΊΠ»Π° } //ΠΊΠΎΠ½Π΅Ρ† Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form> <select id="mk" onchange="mkChange(this);"> <option value="empty">Π’Ρ‹Π±Π΅Ρ€ΠΈΡ‚Π΅ Ρ‚ΠΈΠΏ ΠΌΠ°ΡˆΠΈΠ½Ρ‹</option> <option value="passenger">Π›Π΅Π³ΠΊΠΎΠ²ΠΎΠΉ</option> <option value="BusinessClass">БизнСс класс</option> <option value="Multiplace">ΠœΠ½ΠΎΠ³ΠΎΠΌΠ΅ΡΡ‚Π½Ρ‹ΠΉ</option> <option value="SpecMachinery">Π‘ΠΏΠ΅Ρ†Ρ‚Π΅Ρ…Π½ΠΈΠΊΠ°</option> </select> <select id="brand"> <option value="0">Π’Ρ‹Π±Π΅Ρ€ΠΈΡ‚Π΅ ΠΌΠ°Ρ€ΠΊΡƒ ΠΌΠ°ΡˆΠΈΠ½Ρ‹</option> </select> </form> <img src=""> 

If any ambiguities - ask. Thank you in advance

Closed due to the fact that off-topic participants Vadim Ovchinnikov , Alexey Shimansky , user194374, Regent , aleksandr barakin 10 Jan '17 at 23:29 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • β€œQuestions asking for help with debugging (β€œ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Alexey Shimansky, Community Spirit, Regent, aleksandr barakin
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • show pliz code - L. Vadim
  • 2
    It is not clear what your problem is and how to reproduce it without description and code. - Regent
  • @Aiba probably should have clarified: the code should be in the question itself. And what's the problem? - Regent
  • I can't do it so that when choosing in the second selection with the models, the pictures change depending on the chosen model - Aiba

2 answers 2

You can do it in such a simple way using an object.
PS next time, write less combinations, I looked for pictures for 10 minutes :)

 var mkLists = new Array(4) mkLists["empty"] = ["Π’Ρ‹Π±Π΅Ρ€ΠΈΡ‚Π΅ ΠΌΠ°Ρ€ΠΊΡƒ ΠΌΠ°ΡˆΠΈΠ½Ρ‹"]; mkLists["passenger"] = ["Hyundai", "Toyota Camry 40", "KIA Optima"]; mkLists["BusinessClass"] = ["Toyota Land Cruiser", "Mercedes-Benz S 350-l", "Mercedes-Benz S 500-l", "KIA Cadenza", "Ssangyong Chairman Limo"]; mkLists["Multiplace"] = ["Mercedes Benz Tourismo", "Hyundai Aero City", "Hyundai Universe Luxury", "Hyundai Counti", "Toyota Coaster", "Toyota hiace"]; mkLists["SpecMachinery"]= ["Π“Π°Π· 330232", "Π“Π°Π· 330202", "Камаз 45143", "ΠŸΡ€ΠΈΡ†Π΅ΠΏ (Камаз 45143)"]; function mkChange(selectObj) { var id = selectObj.selectedIndex; var which = selectObj.options[id].value; carsList = mkLists[which]; var carSelect = document.getElementById("brand"); while (carSelect.options.length > 0) { carSelect.remove(0); } var newOption; for (var i=0; i<carsList.length; i++) { newOption = document.createElement("option"); newOption.value = carsList[i]; newOption.text=carsList[i]; try { carSelect.add(newOption); } catch (e) { carSelect.appendChild(newOption); } } checkimage($('#brand').val()); } $('#brand').on('input', function() { checkimage(this.value); }); function checkimage(val) { var arr = { 'Hyundai': 'https://i.stack.imgur.com/WZsJn.jpg?s=48&g=1', 'Toyota Camry 40': 'https://i.stack.imgur.com/HgiaU.gif?s=32&g=1', 'KIA Optima': 'https://www.gravatar.com/avatar/b2f4cbdf639291342efd2d1b65aab9a5?s=32&d=identicon&r=PG&f=1', 'Toyota Land Cruiser': 'https://i.stack.imgur.com/XyqB8.jpg?s=32&g=1', 'Mercedes-Benz S 350-l': 'http://carspravka.ru/images/auto/308364200902ee7538b0f1e820e90dd7.jpg', 'Mercedes-Benz S 500-l': 'https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcQhkicI4gAw25_36ICerF5foHKolNKWo72syMnku1-heg7g8B7U', 'KIA Cadenza': 'http://media.caranddriver.com/images/16q1/665058/2017-kia-cadenza-photos-and-info-news-car-and-driver-photo-667111-s-450x274.jpg', 'Ssangyong Chairman Limo': 'https://madwhips.s3.amazonaws.com/photo_219_ssangyong_chairman_limo_1_33623_original.jpg', 'Mercedes Benz Tourismo': 'https://cdn.riastatic.com/photos/auto/newauto_photos/62/6200/620059/620059f.jpg', 'Hyundai Aero City': 'http://kortim.ru/images/stati/huindai-super-aero-sity/Hyundai_Super_Aero_City540_Euro4.jpg', 'Hyundai Universe Luxury': 'http://fotobus.msk.ru/photo/07/87/06/787067.jpg', 'Hyundai Counti': 'http://a2goos.com/data_images/galleryes/hyundai-county/hyundai-county-05.jpg', 'Toyota Coaster': 'https://upload.wikimedia.org/wikipedia/commons/6/6a/Toyota_Coaster_Deluxe_of_China_TV_BA-227_20100531.jpg', 'Toyota hiace': 'https://upload.wikimedia.org/wikipedia/commons/6/62/Toyota_Hiace_H200_511.JPG', 'Π“Π°Π· 330232': 'http://gruzovo.com/wp-content/uploads/2015/06/330232_1-350x263.jpg', 'Π“Π°Π· 330202': 'http://www.gaz-tranzit.ru/files/auto/img_2_745.jpg', 'Камаз 45143': 'http://westwind-covenant.com/images/59.jpg', 'ΠŸΡ€ΠΈΡ†Π΅ΠΏ (Камаз 45143)': 'http://autoline.com.ua/img/s/gruzovik-gruzovik-zernovozKAMAZ-45143-012-62---4_big--12051014101772788100.jpg' }; if(arr[val] != undefined){ $('img').attr('src', arr[val]); }; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form> <select id="mk" onchange="mkChange(this);"> <option value="empty">Π’Ρ‹Π±Π΅Ρ€ΠΈΡ‚Π΅ Ρ‚ΠΈΠΏ ΠΌΠ°ΡˆΠΈΠ½Ρ‹</option> <option value="passenger">Π›Π΅Π³ΠΊΠΎΠ²ΠΎΠΉ</option> <option value="BusinessClass">БизнСс класс</option> <option value="Multiplace">ΠœΠ½ΠΎΠ³ΠΎΠΌΠ΅ΡΡ‚Π½Ρ‹ΠΉ</option> <option value="SpecMachinery">Π‘ΠΏΠ΅Ρ†Ρ‚Π΅Ρ…Π½ΠΈΠΊΠ°</option> </select> <select id="brand"> <option value="0">Π’Ρ‹Π±Π΅Ρ€ΠΈΡ‚Π΅ ΠΌΠ°Ρ€ΠΊΡƒ ΠΌΠ°ΡˆΠΈΠ½Ρ‹</option> </select> </form> <img src="https://i.stack.imgur.com/WZsJn.jpg?s=48&g=1" width="250"> 

  • What about storing the value (or data-value ) directly as a link to the image? This will simplify the code. - Regent
  • The initial value of src , in an amicable way, should be tied to the original selected <option> using a code. - Regent
  • Yuri thank you for what you need !!! - Aiba
  • @Aiba, not at all :) - Yuri

 $(function() { $(".model").change(function(){ //ΠŸΡ€ΠΈ ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΠΈ ΠΌΠΎΠ΄Π΅Π»ΠΈ var img_id = $(".model").children("option:selected").attr("data"); // ΠŸΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ Π°Ρ‚Ρ€ΠΈΠ±ΡƒΡ‚Π° Π±Π»ΠΎΠΊΠ° с ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ΠΌ ΠΌΠΎΠ΄Π΅Π»ΠΈ автомобиля ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ Π½ΡƒΠΆΠ½ΠΎ ΠΏΠΎΠΊΠ°Π·Π°Ρ‚ΡŒ Π½Π° ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠΈ $(".modelImg").fadeOut(1); // прячСм всС Π±Π»ΠΎΠΊΠΈ с ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ΠΌ Π°Π²Ρ‚ΠΎΠΌΠΎΠ±ΠΈΠ»Π΅ΠΉ $(img_id).fadeIn(10); // ΠΏΠΎΠΊΠ°Π·Ρ‹Π²Π°Π΅ΠΌ Π½ΡƒΠΆΠ½Ρ‹ΠΉ Π±Π»ΠΎΠΊ с ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ΠΌ автомобиля }); }); 
 .modelImg { display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select class="model"> <option>-</option> <option data="#model-1">МодСль 1</option><!-- Π² data ΡƒΠΊΠ°Π·Ρ‹Π²Π°Π΅ΠΌ id Π±Π»ΠΎΠΊΠ° с ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ΠΌ Π½ΡƒΠΆΠ½ΠΎΠ³ΠΎ автомобиля --> <option data="#model-2">МодСль 2</option><!-- Π² data ΡƒΠΊΠ°Π·Ρ‹Π²Π°Π΅ΠΌ id Π±Π»ΠΎΠΊΠ° с ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ΠΌ Π½ΡƒΠΆΠ½ΠΎΠ³ΠΎ автомобиля --> </select> <div class="modelImg" id="model-1"> <!-- id ΠΈΠ΄Π΅Π½Ρ‚Π΅Ρ„ΠΈΠΊΠ°Ρ‚ΠΎΡ€ Π½ΡƒΠΆΠ½ΠΎΠ³ΠΎ изобраТСния Π²Ρ‹Π±Ρ€Π°Π½ΠΎΠ³ΠΎ Π² сСлСктС --> <img src="" alt="МодСль 1" /></div> <div class="modelImg" id="model-2"> <img src="" alt="МодСль 2" /> </div> 

  • fadeOut(1) , in principle, will work (visually) as hide() . Why then use it? Similarly with fadeIn(10) . - Regent