2
We have a radio button with three different values and a select box with several different values, as I could do to multiply the radiobutton checked with the value of the select box and put the result in a field?
Radiobutton code:
<input required="required" value="1" name="diarias" id="diaria1" type="radio"><label
for="diaria">Uma diaria</label>
<input required="required" value="2" name="diarias" id="diaria2" type="radio"><label
for="diaria">Duas diarias</label>
<input required="required" value="3" name="diarias" id="diaria3" type="radio"><label
for="diaria">Três diarias</label><br>
Code of the select box:
<select name="cidade" id="cidade" onclick="calcularopcoes();">
<option name="nenhum" value=""> Escolher </option>
<option name="saopaulo" value="244.00"> São Paulo </option>
<option name="fortaleza" value="412.80"> Fortaleza </option>
<option name="Blumenau" value="412.80"> Blumenau </option>
<option name="riopreto" value="400.90"> Rio Preto </option>
</select>
And the Javascript I tried to do but it didn’t work.
`<script type="text/javascript">
function calcularopcoes(){if(document.getElementById("ajudacusto").value.length < 0){
alert('Por favor, deve se escolher uma opção');
document.getElementById("ajudacusto").focus();
return false
}
if(document.getElementByName("diarias").value.length < 0){
alert('Por favor, deve se escolher uma opção');
document.getElementByName("diarias").focus();
return false
}
// vamos obter o elemento select
var elem = document.getElementById("cidade");
var elem2 = document.getElementByName("diarias");
var selecionou = elem.options[elem.options.selectedIndex]*1 * elem2.options[elem2.options.selectedIndex]*1;
// passa opção selecionada para campo
document.getElementById("ajudacusto").value = selecionou.value;
}
</script>`
What better way can we do it? That the user click on the journal and choose the city he does the multiplication calculation automatically and deposit in the field?
Some HTML is missing. If you can join as well. Do this with pure javascript or is OK with a library?
– Sergio
All this in a form, how so missing some html? Can be with library yes.
– Vinicius
possible duplicate of Change radio button properties from select
– Jorge B.
@Jorgeb. I think the other question should be duplicated of this, and not the other way around, because: 1) This question here was created before; 2) This question here has an accepted answer, the other does not. PS: I didn’t even read the contents of both, I only made one will note if they are even duplicated.
– Math
I think it’s not duplicate because here the goal is just to multiply while in the other question the goal is to change the values of the attributes according to the selection as well.
– Vinicius
Okay, I just thought they looked alike, so I made the suggestion. Nice discussion for the finish line. When a new problem is found in the same code, edit or create new topic?
– Jorge B.
Beware of the chameleon questions.
– Math