5
Hello, I’m programming a system where you average some fields. The point is that not always all values will be informed and the average should calculate the fields in which there are values, that is, the calculation of the average will vary according to the fields yesterday has value.
HTML
<form name="formMediasETEs" method="post" action="AdicionarVazao.php">
<tr class="tabela_dados">
<td>
<input name="data" style="text-align:center; width:150px" style="font-size:10px" type="date" id="data" value="" />
</td>
<td>
<input name="oito" type="text" id="oito" onblur="ver()" style="text-align:center; width: 60px" value="" />
</td>
<td>
<input name="nove" type="text" id="nove" style="text-align:center; width: 60px" value="" />
</td>
<td>
<input name="dez" type="text" id="dez" style="text-align:center; width: 60px" value="" />
</td>
<td>
<input name="onze" type="text" id="onze" style="text-align:center; width: 60px" value="" />
</td>
<td>
<input name="doze" type="text" id="doze" style="text-align:center; width: 60px" value="" />
</td>
<td>
<input name="treze" type="text" id="treze" style="text-align:center; width: 60px" value="" />
</td>
<td>
<input name="quatorze" type="text" id="quatorze" style="text-align:center; width: 60px" value="" />
</td>
<td>
<input name="quinze" type="text" id="quinze" style="text-align:center; width: 60px" value="" />
</td>
<td>
<input name="d_seis" type="text" id="d_seis" style="text-align:center; width: 60px" value="" />
</td>
<td>
<input name="d_sete" type="text" id="d_sete" style="text-align:center; width: 60px" value="" />
</td>
<td>
<input name="d_oito" type="text" onblur="calcularMediaQLSETES()" id="d_oito" style="text-align:center; width: 60px" value="" />
</td>
</tr>
Javascript
function calcularMediaQLSETES() {
var q2 = document.forms['formMediasETEs']['oito'].value;
var q3 = document.forms['formMediasETEs']['nove'].value;
var q4 = document.forms['formMediasETEs']['dez'].value;
var q5 = document.forms['formMediasETEs']['onze'].value;
var q23 = document.forms['formMediasETEs']['doze'].value;
var q6 = document.forms['formMediasETEs']['treze'].value;
var q7 = document.forms['formMediasETEs']['quatorze'].value;
var q8 = document.forms['formMediasETEs']['quinze'].value;
var q9 = document.forms['formMediasETEs']['d_seis'].value;
var q10 = document.forms['formMediasETEs']['d_sete'].value;
var q11 = document.forms['formMediasETEs']['d_oito'].value;
var mqls = (parseFloat(q2) + parseFloat(q3) + parseFloat(q4) +
parseFloat(q5) + parseFloat(q23) + parseFloat(q6) + parseFloat(q7) +
parseFloat(q8) + parseFloat(q9) + parseFloat(q10) + parseFloat(q11)) / 11;
document.forms['formMediasETEs']['mediaQ_ls'].value = mqls.toFixed(2);
}
Thank you!
It’s okay if the solution is with jQuery and not just with Javascript?
– Thiago Lunardi
Yes, it can be! Thank you
– Quito Gaspar
If you can, ask the question also how the HTML is mounted, it can be only the part of the form. This will help people to put together more elaborate solutions, sometimes the "problem" is not only in JS. I see that this can be improved with a looping, removing this lot of variables, but I need to know how HTML is being created.
– Gabriel Katakura