I have a dropdown menu in HTML:
<select id="list" onchange="getSelectValue();">
<option value ="car1">Audi A2</option>
<option value ="car2">Audi A3</option>
<option value ="car3">BMW 1 F20</option>
<option value ="car4">Opel Corsa</option>
</select>
For getting the value of the <select>
, I have this function:
function getSelectValue()
{
var selectedValue = document.getElementById("list").value;
console.log(selectedValue);
}
In another script, I have many arrays:
var car1 =[1, 5, 6, 7];
var car2 =[4, 6, 8, 3];
var car3 =[6, 7, 3, 4];
var car4 =[3, 7, 2, 1];
If the user has chosen eg car 2 in the dropdown menu, the array car2 shall be selected. And now eg values of this array should be added. How can I do this?
document.write(car[1]+car[3]);
For something like this, you can use an object. The selected value is the key of the object, and then at the key is the array you want, like this:
var carData = { "car1": [1, 5, 6, 7], "car2": [4, 6, 8, 3], "car3": [6, 7, 3, 4], "car4": [3, 7, 2, 1] }; function getSelectValue() { var selectedValue = document.getElementById("list").value; console.log(selectedValue); console.log(carData[selectedValue]); }
<select id="list" onchange="getSelectValue();"> <option value="car1">Audi A2</option> <option value="car2">Audi A3</option> <option value="car3">BMW 1 F20</option> <option value="car4">Opel Corsa</option> </select>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.