I have two select
s that sends to event change
the value of data-id
and assignment to variable tipo
.
I have two objects each with values in the keys 1:
and 2:
.
Using tipoUm[1]
returns me the value 1.10
normal. But if I use the tipo
variable to access the object, this way tipo[1]
, I can not access the object, but I am returned the second string of the tipo
variable, ie i
tipoUm
or tipoDois
).
How can I get the value of the 1:
key, for example, from the objects according to the value of the tipo
variable in the event?
var tipoUm = {
1: 1.10,
2: 2.50,
};
var tipoDois = {
1: 2.20,
2: 4.70,
};
$(".tipo").on('change', function(){
var tipo = $(this).data("id");
console.log(tipo);
// a variável "tipo" pode ser: tipoUm ou tipoDois
// pegar o valor da chave "1" do objeto de acordo com a
// variável "tipo"
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><selectclass="tipo" data-id="tipoUm">
<option value="">Selecione</option>
<option value="1">Opção 1</option>
</select>
<select class="tipo" data-id="tipoDois">
<option value="">Selecione</option>
<option value="1">Opção 1</option>
</select>