Jump to content

Recommended Posts

Estimados

 

Solicito su ayuda por que ando en busqueda de como hacer una mini calculadora, como por ejemplo, en un formulario ingreso xx cantidad multiplicado por xxx cantidad, me arroje xx resultado.

 

Espero que me haya explicado bien.

 

Saludos

Link to comment
Share on other sites

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

 

<select id="opt">

<option value="1">+</option>

<option value="2">-</option>

<option value="3">x</option>

<option value="4">/</option>

</select>

 

<input type="text" id="b"><input type="button" onclick="operar()" value="Calcular"><br/>

 

<input type="text" id="result" value="Resultado aqui">

 

<script type="text/javascript">

function operar(){

n1=parseInt(document.getElementById("a").value);

n2=parseInt(document.getElementById("b").value);

opt = parseInt(document.getElementById("opt").value);

if(opt == 1){

r=n1+n2;

}

else if(opt == 2){

r=n1-n2;

}

else if(opt == 3){

r=n1*n2;

}

else{

if(n2 == 0){ r="Error"; }else{ r=n1/n2; }

}

document.getElementById("result").value=r;

}

 

</script>

Edited by cañangasñangas
Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...