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 2 of 2
  1. #1
    Regular Coder Jesper Møller's Avatar
    Join Date
    Jun 2006
    Location
    Denmark, Copenhagen
    Posts
    138
    Thanks
    9
    Thanked 0 Times in 0 Posts

    multiple calculation with different prices

    Hi
    Im working on a form where the user can chose how many items the want.
    most items cost 20 but some cost 35. so im trying to to make a script that will calculate the total numbers of item and the total price
    items with pice of 30 have a class="mad1" and items with pice of 35 have a class="mad2" on the inputfields

    so far i have come up with this :

    Code:
                var antal = 0;
                var antalf = 0;
                var antalr = 0;
                var subtotal = 0;
                var price = 30;
        $(".mad1, .mad2").each(function() {
            //add only if the value is number
            if (this.value.trim().length != 0 && !isNaN(this.value.trim()) ) {
            //Set max to 25
            if (this.value >= 25){
                this.value= 25
                }
            if($(this).hasClass("mad2")){
                price = 35;
                }
            else{
                price = 30;
                }
                antal += parseFloat(this.value);
                subtotal = antal*price;
                $(this).css("background-color", "#ffffff");
            }
            else if (this.value.length != 0){
                 $(this).css("background-color", "#e89898");
            }
        });
                $("#antal").html(antal.toFixed(0));
                $("#antalf").html(antalf.toFixed(0));
                $("#antalr").html(antalr.toFixed(0));
                $("#subtotal").html(subtotal.toFixed(0));
    But the price for all items is calculatede as if they are 35 ??
    What am i doing wrong

    The page im working on is Gordion - Online Bestilling
    Last edited by Jesper Møller; 04-02-2014 at 10:53 PM.
    "True knowledge exists in knowing that you know nothing."

    "Education is learning what you didn't even know you didn't know!"

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,105
    Thanks
    23
    Thanked 594 Times in 593 Posts
    Most of the things you say like
    most items cost 20 but some cost 35.
    and
    items with price of 30 have a class="mad1" and items with price of 35 have a class="mad2"
    don't appear to be true in your code here or on your page.
    No Problem a subtotal can be gotten by grabbing the cost and multiplying it be the amount.
    If you do not understand something in the code below feel free to ask. This has two functions, the first makes sure the user enters a number and the multiples this by the value in the price column.
    The second function adds up the sub totals to keep a running total. This is fixed and drops down with the page.
    A problem with the code is; it wont refigure the subtotal if you back space a number and delete the value there. I thought of how to do that
    This show only part of your big table
    Have fun:
    Code:
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta charset="UTF-8" />
    <title>Title of the document</title>
    <style type="text/css">
    #title{
    	position:fixed;
    	top:38px;
    	left:550px;
    	outline:1px solid black;
    	text-align:center;
    	height:8px;
    	width:65px;
    }
    #demo{
    	position:fixed;
    	top:60px;
    	left:550px;
    	outline:1px solid black;
    	text-align:center;
    	height:83px;
    	width:65px;
    	padding-top:25px;
    }
    </style>
    </head>
    <body>
    <table id="my_table" border="1">
    <tr>
    	<td><h3 id="fugl">POULTRY</h3></td>
    	<td class="remember" style="text-align: center;">&nbsp;white bread&nbsp;</td>
    	<td class="remember" style="text-align: center;">&nbsp;rye bread&nbsp;</td>
    	<td class="menukr">a Kr.</td>
    	<td class="menukr">Sub total</td>
    </tr>
    <tr>
    	<td><h1>Cooked chicken</h1>
    	with pineapple, bacon and curry mayaise</td>
    	<td class="antal"><input  id="poulty_cooked_1" name="Cooked chicken on white bread" value="0" onkeyup="tally('poulty_cooked_1');" onfocus="this.value=''" style="width: 50px; background-color: rgb(255, 255, 255);" type="number"></td>
    	<td class="antal"><input  id="poulty_cooked_2" name="Cooked chicken on rye bread" value="0" onkeyup="tally('poulty_cooked_2');" onfocus="this.value=''" style="width: 50px; background-color: rgb(255, 255, 255);" type="number"></td>
    	<td id="poulty_cooked_price">30,-</td>
    	<td id="poulty_cooked_sub" style="width: 50px;">0</td>
    </tr>
    <tr>
    	<td><h1>Boiled chicken</h1>
    	with pineapple, bacon and mushrooms</td>
    	<td class="antal"><input  id="poulty_boiled_1" name="Boiled chicken on white bread" value="0" onkeyup="tally('poulty_boiled_1');" onfocus="this.value='';" style="width: 50px; background-color: rgb(255, 255, 255);" type="text"></td>
    	<td class="antal"><input  id="poulty_boiled_2" name="Boiled chicken on rye bread" value="0" onkeyup="tally('poulty_boiled_2');" onfocus="this.value=''" style="width: 50px; background-color: rgb(255, 255, 255);" type="text"></td>
    	<td id="poulty_boiled_price">30,-</td>
    	<td id="poulty_boiled_sub" style="width: 50px;">0</td>
    </tr>
    </table>
    <div id="title" >Total Kr</div>
    <div id="demo" ></div>
    
    <script type='text/javascript' src='javascript/jquery.js'></script>
    <script type="text/javascript">
    var tot = 0;
    var dolly = 0;
    function tally(basic){
    	if(isNaN(document.getElementById(basic).value))
    		document.getElementById(basic).value = 0;
    	basic = basic.substring(0, basic.length-1);
    	add_em = Number(document.getElementById(basic+"1").value) + Number(document.getElementById(basic+"2").value);
    	cost = document.getElementById(basic+"price").innerHTML;
    	cost = Number(cost.substring(0, cost.length-2));
    	document.getElementById(basic+"sub").innerHTML = add_em*cost;
    	grand(basic);
    }
    function grand(place){
    	var x = document.getElementById("my_table").rows.length;
    	for(y=0; y<x;y++){
    		dolly += Number(document.getElementById("my_table").rows[y].cells[4].innerHTML);
    		if (isNaN(dolly)) dolly = 0;
    	}
    	document.getElementById("demo").innerHTML = "<b>"+dolly+",- Kr</b>";
    	dolly=0;
    }
    </script>
    </body>
    </html>
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."


  •  

    Tags for this Thread

    Posting Permissions

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