2
I have two select
s sent to the event change
the value of data-id
and attribute to variable tipo
.
I have two objects each with values in the keys 1:
and 2:
.
Using tipoUm[1]
me returns the value 1.10
normal. But if I use the variable tipo
to access the object in this way tipo[1]
, I cannot access the object, but I return the second string of the variable tipo
, i.e., the i
(of tipoUm
or tipoDois
).
How do I get the key value 1:
, for example, the objects according to the variable value tipo
at 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>
<select class="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>
Really. Actually the scope of the objects was local, but resolved quiet.
– Sam
With
eval
or withwindow
? I’m glad you solved the problem ;)– João Pedro Henrique
used window even :D
– Sam