Multiply Values in JAVASCRIPT

Asked

Viewed 2,412 times

1

Guys I wanted to catch the value Field where you place the value of R$ and limit [tipo (1<= 99 ) *23 e (>100) *25 ] has as ?

Code I have:

<!DOCTYPE html>
<html>
<head>
       <script src="jquery.min.js" type="text/javascript"></script>
    <script src="jquery.maskMoney.js" type="text/javascript"></script>
    <style>
        * { margin: 0; padding: 0; font-size: 13px; font-family: Verdana;}
        #main { width: 1500px; height: 1800px; margin: auto;}
        section { margin-top: -385px; margin-left: 608px; }
        #demo3 { display: none;}
    </style>
</head>
<body>
<script> 
function soma() 
{
form.campo4.value = parseInt(form.campo1.value*1) * parseInt(form.campo2.value*1) 
}
</script>
<script type="text/javascript">
$(function(){
 $("#demo4").maskMoney({symbol:'R$ ', 
showSymbol:true, thousands:'', decimal:'.', symbolStay: false});
 })

 $(function() {
   $(document).on('click', 'input[value][id=demo4]', function() {
     this.select();
   });

 });
 $("#demo4").bind('input propertychange', function(){
    if($(this).val() > 4){
         $(this).val() = 4;
    }else if($(this).val() < 1){
         $(this).val() = 1;
    }
});

</script>
<script>
  function limitarInput(obj) {
    obj.value = obj.value.substring(0,8);
     }
</script>

<div id="main">
<section>
<form name="form">
<input name="campo1" id="demo4" value="00,00" onkeyup="limitarInput(this)" style="width: 200px; height: 50px; background-color: transparent; font-size: 30px; text-align: center; color: darkolivegreen"><br> 

<input name="campo2" value="12" id="demo3"><br>  

    <input type="button" onclick="soma()" value="CALCULAR"
       style="width: 200px; height: 40px; background-color: transparent; font-size: 30px; margin-top: 26px; cursor: pointer; text-align: center; color: transparent; border: 1px solid black;">

<input name="campo4" readonly id="resultado" value=""  style="width: 200px; height: 50px; background-color: transparent; font-size: 30px; margin-top: 0px; display: block; text-align: center; color: maroon"><br>
    </form>
</section>
</div>
</body>
</html>
  • Is there any way to explain this condition of limitation? She was confused.

  • Like Anderson this here look

  • http://imgur.com/a/dnoax

  • This image code, at first, seems to work. So what’s the problem?

  • Already solved the Friend Leo Caracciolo already solved my problem thanks | Anderson Carlos Woss for help :)

1 answer

3


Just compare the value of the input and impose a condition

function soma() 
{
	var valor;
	var campo = form.campo1.value;
	if(campo >=1  && campo < 100){
		valor=23;
	}else{
		valor=25;
	}
	form.campo4.value = parseInt(campo) * parseInt(valor) 
}

  function limitarInput(obj) {
    obj.value = obj.value.substring(0,8);
  }
<div id="main">
<section>
<form name="form">
<input name="campo1" id="demo4" value="00,00" onkeyup="limitarInput(this)" style="width: 200px; height: 50px; background-color: transparent; font-size: 30px; text-align: center; color: darkolivegreen"><br>  

    <input type="button" onclick="soma()" value="CALCULAR"
       style="width: 200px; height: 40px; background-color: transparent; font-size: 30px; margin-top: 26px; cursor: pointer; text-align: center; color: transparent; border: 1px solid black;">

<input name="campo4" readonly id="resultado" value=""  style="width: 200px; height: 50px; background-color: transparent; font-size: 30px; margin-top: 0px; display: block; text-align: center; color: maroon"><br>
    </form>
</section>
</div>

If you want to use decimal part, as noted by Anderson Carlos Woss comment, and that can give a substantial difference in the result, use parseFloat in place of parseInt

function soma() 
{
	var valor;
	var campo = form.campo1.value;
	if(campo >=1  && campo < 100){
		valor=23;
	}else{
		valor=25;
	}
	form.campo4.value = parseFloat(campo) * parseFloat(valor) 
}

  function limitarInput(obj) {
    obj.value = obj.value.substring(0,8);
  }
<div id="main">
<section>
<form name="form">
<input name="campo1" id="demo4" value="00,00" onkeyup="limitarInput(this)" style="width: 200px; height: 50px; background-color: transparent; font-size: 30px; text-align: center; color: darkolivegreen"><br>  

    <input type="button" onclick="soma()" value="CALCULAR"
       style="width: 200px; height: 40px; background-color: transparent; font-size: 30px; margin-top: 26px; cursor: pointer; text-align: center; color: transparent; border: 1px solid black;">

<input name="campo4" readonly id="resultado" value=""  style="width: 200px; height: 50px; background-color: transparent; font-size: 30px; margin-top: 0px; display: block; text-align: center; color: maroon"><br>
    </form>
</section>
</div>

  • A small observation: perhaps it is interesting to use the parseFloat, whereas the value refers to a price and may contain decimal parts.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.