As the code with several textfields shows, I want to make visible just dataEntrada, dataSaida and KM and from these 3 fill in the others that will be invisible, the amount to be paid will be displayed in an alert when you click on simulate. The account is working, but I need to be clicking on the textfield fields to be able to fill in, so the calculation goes out blank.
function funcao1()
{
var form3 = document.getElementById('simulador');
var valorreal = document.getElementById('valorreal');
form3.addEventListener('submit', function(e) {
// alerta o valor do campo
alert("O valor total a ser pago será de: " + "R$" + valorreal.value);
// impede o envio do form
e.preventDefault();
});
}
function calcula_valor(){
document.form3.valortotal.value = document.form3.KM.value * document.form3.valorKM.value;
//document.form3.valortotal.value = ((document.form3.dataSaida.value - document.form3.dataEntrada.value) * document.form3.diaria.value) + (document.form3.KM.value * document.form3.ValorKM.value);
}
function difDias(){
var dataSaida = new Date(document.getElementById("dataSaida").value);
var dataEntrada = new Date(document.getElementById("dataEntrada").value);
return parseInt((dataSaida - dataEntrada) / (24 * 3600 * 1000));
}
function chamar(){
document.getElementById("ndias").value = isNaN(difDias()) ? "" : difDias();
}
function calcula_aluguel(){
document.form3.valoraluguel.value = document.form3.ndias.value * document.form3.diaria.value;
}
function calculo_total(){
document.form3.valorreal.value = (document.form3.valoraluguel.value*1) + (document.form3.valortotal.value*1);
}
<section class="bloco3">
<div id="title3">
<p id="title3">Simulador</p>
</div>
<div id="simulador">
<div id="form3">
<form class="bloco3" name="form3" action="" method="post">
<input type="date" id="dataEntrada" name="dataEntrada" placeholder="dataEntrada" onchange="chamar()">
<input type="date" id="dataSaida" name="dataSaida" placeholder="dataSaida" onchange="chamar()">
<input type="text" id="ndias" name="ndias" value="" placeholder="nº dias">
<input type="hidden" id="diaria" name="diaria" value="50" required="" placeholder="Diaria">
<input type="text" id="valoraluguel" name="valoraluguel" value="" onclick="calcula_aluguel()" onkeyup="calcula_aluguel()" placeholder="valor da diaria">
<input type="hidden" id="valorKM" name="valorKM" value="2" placeholder="valorKM">
<input type="text" id="KM" name="KM" value="" onclick="calcula_valor()" onkeyup="calcula_valor()" placeholder="digite o KM percorrido">
<input type="text" id="valortotal" name="valortotal" value="" placeholder="valor total do KM percorrido">
<input type="text" id="valorreal" name="valorreal" value="" onclick="calculo_total()" onkeyup="calculo_total()" placeholder="valor total a ser pago">
<input type="submit" onclick="return funcao1()" onkeyup="funcao1()" value="Simular" />
</form>
</div>
</div>
</section>