I have a small form that gives a result based on the input of two values via form. I just want the impression of the result to be made out of the form of the result. I've tried everything and I can not. There are 3 possibilities:
The result appears: [object][Object]
: what happened a few times when I change the schedule;
Read the "else" for the "third if" and the sentence appears: Please redo your query. This occurs when I have the sentence: - var resultado = $("#valor3").val();
- (most common; it does not 'calculate' and understands that the value is null);
'undefined' that occurs when I have this expression: var resultado = $("#resultado").val();
I leave the code for review, I'm quite a beginner in JavaScript:
CODE (file in .html) - I leave in the form 'undefined'
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Documento sem título</title>
<style type="text/css">
.btn { }
.n1 { }
.n2 { }
.n3 { }
</style>
<script type="text/javascript" src="jquery-1.8.3.js"></script>
<script type="text/javascript" src="jquery.form.js"></script>
<script type="text/javascript">
function somarValores(){
var s1 = document.getElementById("s1").value;
var valor1 = convToFloat(s1) * 0.50;
var s2 = document.getElementById("s2").value;
var valor2 = convToFloat(s2) * 12;
var valor3 = valor1 + valor2;
document.getElementbyClassName(resultado).value = valor3;
}
function convToFloat(temp) {
var value;
if (temp.indexOf(',') == -1) {
temp = temp.replace(",", ".");
}
return parseFloat(temp);
}
$(document).ready(function(){
$(".btn").click(function(){
var volume = $("#valor1").val();
if(volume != 0){
$("#n1").html("<font color='#828282' face='arial'><b>RELATÓRIO</font><br><br>Volume do ambiente: <font color='blue'>" + volume + " m³</b></font>");
}else{
$("#n1").html("<font color='#828282' face='arial'><b>RELATÓRIO</font><br><br>Campo <i>Volume</i> não preenchido!</b>");
}
var pessoa = $("#valor2").val();
if(pessoa != 0){
$("#n2").html("<b>Número de pessoas: <font color='blue'>" + pessoa + "</b></font>");
}else{
$("#n2").html("<b>Campo <i>Número de Pessoas</i> não preenchido!</b>");
}
var resultado = $("#resultado").val();
if(resultado != 0){
$("#n3").html("<b>Número de pessoas: <font color='blue'>" + resultado + "</b></font>");
}else{
$("#n3").html("<b>Por favor, refaça sua consulta!</b>");
}
return false;
})
})
</script>
</head>
<body>
<br>
<table width="30%" border="0">
<tr>
<td width="30%" align="center" valign="center" bgcolor="#EEEEE0">
<font color="#8B8B83" face="arial"><b> - Estimativa de Carga Térmica - </b></font>
</td></tr></table><br>
<table width="30%"><tr><td width="30%">
<form method="post" action""><fieldset><legend></legend>
<table width="100%" border="0">
<tr>
<td width="50%" align="right" valign="center">
<b>Volume ( m³): </b>
</td>
<td width="50%" align="center" valign="center">
<input type="text" name="volume" id="valor1" size="5"/></td></tr>
<tr>
<td width="50%" align="right" valign="center">
<b>Nº de Pessoas: </b>
</td>
<td width="50%" align="center" valign="center">
<input type="text" name="pessoa" id="valor2" size="5"/></td></tr>
<tr>
<td width="50%" align="center" valign="center">
<br><br>
</td>
<td width="50%" align="center" valign="center">
<input type="text" name="resultado" id="valor3" hidden/>
<input type="submit" value="Calcular Carga Térmica" class="btn"/>
</td></tr></table></fieldset>
<br>
<dd><div id="n1"><p></p></div><br>
<div id="n2"><p></p></div><br>
<div id="n3"><p></p></div></dd>
</form>
</td></tr></table>
</body>
</html>