IF condition according to option field selection

Asked

Viewed 487 times

2

I’m new to programming, I’m having a problem with my code:

var micro = "Micro Inversores";

if (micro.equals(document.getElementById("tipo_unidade"))) {
    var tipo1 = 1;
else
    var tipo1 = 0.8;
}
<select id="tipo_unidade" name="tipo_unidade" onchange="MudaLabeL('compl_Preco_1',txt_qtd_1.value)" style="width:660px">
    <option value="Micro Inversores">Micro Inversores</option>
    <option value="Inversor Tradicional">Inversor Tradicional</option>

I have a field <option> where the user selects an option, according to its selection, need the value of the variable tipo1 be it 0.8 or 1.

I tried too:

var char micro = "Micro Inversores";

if (micro = p_classe) {
    var tipo = 1;
else
    var tipo = 0.8;
}

document.getElementById("p_classe").innerHTML = (classeundValue);

function atualizaclasse() {
    var classeund = document.getElementById("tipo_unidade");
    classeundValue = classeund.value;
}

If I remove the condition part if and define, for example var tipo = 1;, the code works perfectly. My problem is in setting this value of 0.8 or 1 for the type variable, according to the user’s selection.

  • 3

    I think you need to take the value of select with . value: document.getElementById("tipo_unidade").value

4 answers

2


I set an example with the value as @Sam said

var micro = "Micro Inversores";
var valor = 0; // Veja que eu coloquei a variável valor em um escopo global

	function addEventHandler(elem, eventType, handler) {
		if (elem.addEventListener)
			elem.addEventListener (eventType, handler, false);
		else if (elem.attachEvent)
			elem.attachEvent ('on' + eventType, handler); 
	}

	addEventHandler(document, 'DOMContentLoaded', function() {
		addEventHandler(document.getElementById('tipo_unidade'), 'change', function() {
			if (document.getElementById("tipo_unidade").value == micro) {
				valor = 1;
			} else {
				valor = 0.8;
			}
			console.log(valor);
			document.getElementById('compl_Preco_1').value = valor;
		});
	});
<select id="tipo_unidade" name="tipo_unidade" style="width:660px">
    <option value="">Selecione uma opção</option>
	<option value="Micro Inversores">Micro Inversores</option>
	<option value="Inversor Tradicional">Inversor Tradicional</option>
</select>

<input type="text" id="compl_Preco_1">

  • I see that your code works here in stackoverflow, it’s a little complex for me, but the result is that you assign 1 to the variable "value" if type_unit equals micro and 0.8 if it’s not, when I put it in Cód, everything runs normally without errors, but if I use this "value" variable in the formula, then Cód n performs nothing. var Total = ((Material*(value)) + Mobra + tax + Freight + Liq + Project + RT + General Expenses + Severanscabdisj) * 1;

  • The variable valor does not exist outside the scope of the function, that is, it exists only there if what you can do is put the variable valor in another scope, I will change the answer and put it this new way, so I believe you will be able to use

1

If I understood your question correctly, you could use the values you need as value of each option:

<select id="tipo_unidade" name="tipo_unidade" onchange="MudaLabeL('compl_Preco_1',txt_qtd_1.value)" style="width:660px">
    <option value="1">Micro Inversores</option>
    <option value="0.8">Inversor Tradicional</option>
</select>

Hence:

let tipo = parseFloat(document.getElementyById('tipo_unidade').value);

I did not test, but I believe it is the way. (Within what I understood of your question.)

0

You can do it like this:

function teste(a) {
     var micro = (a.value || a.options[a.selectedIndex].value);  
     var tipo1 = 0;
     
     if (micro == "Micro Inversores"){
           tipo1 = 1;
     }else if(micro == "Inversor Tradicional"){
           tipo1 = 0.8;
     }else{
           tipo1 = 0;
     }
    
    document.getElementById('compl_Preco_1').value = tipo1;
}
<select onChange="teste(this)" id="tipo_unidade" name="tipo_unidade"  style="width:660px">
      <option value="">-Selecione</option>
     <option value="Micro Inversores">Micro Inversores</option>
     <option value="Inversor Tradicional">Inversor Tradicional </option>
</select>

<input type="text" id="compl_Preco_1">

0

thank you very much for the help, incredible as it may seem, it was something silly on Cód (as always) that was making everything go wrong.

        #COMO FUNCIONOU ↓
        var micro = "Micro Inversores";
        if (document.getElementById("tipo_unidade").value == micro) {
            var tipon = 1;
        } else {
            var tipon = 0.8;
        }

        #CÓDIGO DANDO PROBLEMAS ↓
        var micro = "Micro Inversores";
        if (micro.equals(document.getElementById("tipo_unidade"))) {
        var tipo1 = 1;
        else
        var tipo1 = 0.8;
        }

I did not get to test with the equals, the rule is clear, whether it worked leaves working (haha), but I had forgotten to close and open keys in if '-' } LSE {

I made a big deal out of it, but anyway, thank you so much for trying to help me!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.