View Full Version : Javascript Operations

11-02-2012, 05:06 PM
Hi all,

I'm editing a website form to a friend, but the js page is a deadzone for me.

I have a html page named:


<div id="rendimentos_mensais" class="seccao_form">
<span><b>Proveitos:</b><b class="notas_form"> (1)</b></span><br>
<p><span>Vendas:</span><input name="prov_vendas" id="prov_vendas" class="numeric" type="text" value="0"></p>
<p><span>Prestação de Serviços:</span>
<input name="prov_ps" id="prov_ps" class="numeric" type="text" value="0"></p>


<p><span>Margem:</span><input name="prov_mrg" id="prov_mrg" class="percent" type="text" value="0"></p>


<p><span>Valor Compras:<b class="notas_form"> (2)</b></span><input name="prov_comp" id="prov_comp" class="numeric" type="text" value="0"></p>

<p><span><b>MARGEM BRUTA:</b></span><input name="mar_brut_prov" id="mar_brut_prov" type="hidden">
<input id="mar_brut_prov_output" type="text" readonly class="output"></p>


<b class="notas_form">(1) - Valores Médios Mensais</b>

<b class="notas_form">(2) - Deve colocar o valor correcto das compras de acordo com a margem</b>


In the website you can see this:

Then i have a js file with this:

$.watermarker.setDefaults({ color: '#737270', left: 0, top:0 });

function calcTotal()
var total = 0;

total = new Number($("#mar_brut_prov").val()-$("#tot_custo_mensal").val()-($("#tot_end_prestacao").val()/12));

$("#rendimentos_mensais input[type='text']").not(".output").change(function(){
var total = 0;
$("#rendimentos_mensais input[type='text']").not(".output").each(function(){
total += new Number($(this).val().replace(/\s/g,'').replace(',','.'));
$("#mar_brut_prov_output").val(number_format(total,2,',',' ')+" €");

So the js code is adding all the values of:
prov_vendas + prov_ps + prov_mrg + prov_comp

But that is not what i need. I need adding and subtracting, like this:
prov_vendas + prov_ps - prov_comp
Ignoring the prov_mrg, because is a percentage for another thing.

11-03-2012, 10:28 AM
Replace this

$("#rendimentos_mensais input[type='text']").not(".output").each(function(){
total += new Number($(this).val().replace(/\s/g,'').replace(',','.'));

with this

var prov_vendas = Number($('#prov_vendas').val().replace(/\s/g,'').replace(',','.'));
var prov_ps = Number($('#prov_ps').val().replace(/\s/g,'').replace(',','.'));
var prov_comp = Number($('#prov_comp').val().replace(/\s/g,'').replace(',','.'));
total = prov_vendas + prov_ps - prov_comp;

rnd me
11-03-2012, 11:14 PM
don't use "new " with Number, just Number is shorter, and you want numbers, not objects.

>typeof new Number(5)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum