dimanche 29 novembre 2015

JavaScript Calculating wrong

I am trying to perform calculation using JavaScript. When user enter less than 10 pages in input (#page) the cost is 1. if he adds more than 10, each page costs .10. there are 2 options for checkbox, if he clicks first checkbox 10 is added and second checkbox 15 is added.

This is working when it is done in sequential steps. (input then clicking checkbox).

Ex: Input is : 9 (total: 1) click checkbox1 - duplicates (total : 11) click checkbox1 - laser (total: 26)

Now if i change the Input to 11, then the total becomes 1.10 - even if both the checkboxes are checked.. (expected result should be - 26.10)

I am not sure how to do this...can anyone help me out

<html>
<head>
    <title>Calculation</title>
</script>
    <script>
        function calculate()
        {
            var pages=document.getElementById("page").value;

            if(pages <=10)
            {
                total.value=1;
            }
            if(pages >=11)
            {
                var extra_pages= pages - 10;
                var new_total= extra_pages * .10;
                var new_total1= 1 + new_total;
                total.value= new_total1; 
            }
        }

        function checkbox1()
        {
            if(document.getElementById("ckbox1").checked === true)
            {
                var total1=document.getElementById("total").value;
                const add1 = 10;
                var check1 = +total1 + +add1;
                total.value=check1;
            }
            if(document.getElementById("ckbox1").checked === false)
            {
                var total1=document.getElementById("total").value;
                 const sub1 = 10;
                 var check2 = +total1 - +sub1;
                 total.value = check2;
            }
        }

        function checkbox2()
        {
            if(document.getElementById("ckbox2").checked === true)
            {
                var total1=document.getElementById("total").value;
                const add1 = 15;
                var check1 = +total1 + +add1;
                total.value=check1;  
            }
            if(document.getElementById("ckbox2").checked === false)
            {
                 var total1=document.getElementById("total").value;
                 const sub1 = 15;
                 var check2 = +total1 - +sub1;
                 total.value = check2;
            }
        }
</script>
<body>
     Enter a Number: <input type="text" id="page" value="1" oninput="calculate()">
    <br>
    <br><br><br><br>
       duplicates <input type="checkbox" id="ckbox1" onclick="checkbox1()">
    laser print: <input type="checkbox" id="ckbox2" onclick="checkbox2()"> <br><br>
</body>
</html>




Aucun commentaire:

Enregistrer un commentaire