Reputation: 33
I have a dropdown list right here and I have declared a variable (vki)
<html>
<body>
<form>
Select your favorite letter!
<select id="Fletter">
<option selected disabled>Choose one</option>
<option>A</option>
<option>B</option>
<option>C</option>
<option>D</option>
<option>E</option>
<option>F</option>
</select>
</form>
<script>
var vki,
</script>
</body>
</html>
I want (vki) to have different values when different option is selected. For example, vki=5 when A is selected, vki=7 when B is selected, how would I do that?
Thank you!
Upvotes: 1
Views: 604
Reputation: 781004
Use an object to map the option values to the values you want to put in vki
.
$("Fletter").change(function() {
var vki_map = {
A: 5,
B: 7,
...
}
vki = vki_map[$(this).val()];
});
But I wonder why you don't just put these values in the <option>
directly, e.g.
<option value="5">A</option>
<option value="7">B</option>
...
Then you could do:
vki = parseInt($(this).val, 10);
Upvotes: 3