Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript Operations

    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:

    company_simulation.html
    Code:
    ....
    <body>
    .....
    <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>
                          
                          <br>
                          
    <p><span>Margem:</span><input name="prov_mrg" id="prov_mrg" class="percent" type="text" value="0"></p>
                        
                        <br>
                        
    <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>
    
    <br>
    
    <b class="notas_form">(1) - Valores Médios Mensais</b>
    <br>
    
    <b class="notas_form">(2) - Deve colocar o valor correcto das compras de acordo com a margem</b>
    </div>
    
    ....
    </body>
    .....
    In the website you can see this:


    Then i have a js file with this:
    Code:
    $(window).load(function(){
    	$.watermarker.setDefaults({ color: '#737270', left: 0, top:0 });
    	$(".jq_watermark").watermark();
    	$('.numeric').numeric(null);
     	
     	function calcTotal()
    	{
    		var total = 0;
    		
    		total = new Number($("#mar_brut_prov").val()-$("#tot_custo_mensal").val()-($("#tot_end_prestacao").val()/12));
    		$("#total").val(parseFloat(total));
    	}
     		
    	$("#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").val(total);
    		calcTotal();
    		$("#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.

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Replace this
    Code:
    $("#rendimentos_mensais input[type='text']").not(".output").each(function(){
    	total += new Number($(this).val().replace(/\s/g,'').replace(',','.'));		
    });
    with this
    Code:
    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;
    Last edited by devnull69; 11-04-2012 at 09:33 AM.

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,461
    Thanks
    11
    Thanked 600 Times in 580 Posts
    don't use "new " with Number, just Number is shorter, and you want numbers, not objects.

    Code:
    >typeof new Number(5)
    "object"
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •