Jump to content

Checkbox Selection


erme

Recommended Posts

Where do you calculate the total? In what language?

 

My first thought it... wherever you check to see which checkboxes are checked...

Do a loop. Make each checkbox go through the loop. For each loop iteration, check that the checkbox is checked. If it is, increment a variable (let's say "i") by one.

After the loop, check the value of "i". Then it's an if statement... if i => 5, then total = total * .98 ... or however you're calculating things.

Link to comment
https://forums.phpfreaks.com/topic/213903-checkbox-selection/#findComment-1113289
Share on other sites

The best way to do this really varies on the rules for the discounts. Does it have to be strictly 5, or it can be more? Assuming it can be 5 or more, and you want to easily add/remove more discounts, a possible solution:

 

//var total = ... (assume this has the total check boxes checked)
var discounts = {5:2, 10:4, 20:8}
var discount = 0;

for (var requirement in discounts)
{
    if (total < requirement)
    {
        break;
    }
    discount = discounts[requirement];
}

Link to comment
https://forums.phpfreaks.com/topic/213903-checkbox-selection/#findComment-1113293
Share on other sites

I am by no means an expert in javascript. I found a bit of jQuery

 

            $(document).ready(function() {
                var total = 0;  
     
                function calcTotal() { 
                    $("input:checked").each(function() { 
                        var value = $(this).attr("value"); 
                        total += parseFloat(value);  
                    }); 
                } 
    
                calcTotal();     
             
                $("input:checkbox, input:radio").click(function() { 
                    total = 0;  
                    calcTotal();  
                    $("#total").val($.format('{a:.2f}', { a:total }));
                });
            });

 

The discounts are 2% off the total per each additional item selected. Eg if 6 items are selected then its 2% off the total, 7 items then its 4% off etc .. max of 15 selectable

Link to comment
https://forums.phpfreaks.com/topic/213903-checkbox-selection/#findComment-1113295
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.