erme Posted September 20, 2010 Share Posted September 20, 2010 Hi, Im 'trying' to write a checkbox type calculator. How can I do something along the lines of, If 5 checkboxes are checked, apply a 2% discount to the total. Many thanks Quote Link to comment https://forums.phpfreaks.com/topic/213903-checkbox-selection/ Share on other sites More sharing options...
Miss_Rebelx Posted September 20, 2010 Share Posted September 20, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/213903-checkbox-selection/#findComment-1113289 Share on other sites More sharing options...
Adam Posted September 20, 2010 Share Posted September 20, 2010 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]; } Quote Link to comment https://forums.phpfreaks.com/topic/213903-checkbox-selection/#findComment-1113293 Share on other sites More sharing options...
erme Posted September 20, 2010 Author Share Posted September 20, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/213903-checkbox-selection/#findComment-1113295 Share on other sites More sharing options...
Adam Posted September 20, 2010 Share Posted September 20, 2010 How are you naming the check boxes? Quote Link to comment https://forums.phpfreaks.com/topic/213903-checkbox-selection/#findComment-1113301 Share on other sites More sharing options...
erme Posted September 20, 2010 Author Share Posted September 20, 2010 Through value <input type="checkbox" value="4.95" /> I've included jQuery strings Quote Link to comment https://forums.phpfreaks.com/topic/213903-checkbox-selection/#findComment-1113304 Share on other sites More sharing options...
erme Posted September 21, 2010 Author Share Posted September 21, 2010 Or do you know where I can find a better way? Quote Link to comment https://forums.phpfreaks.com/topic/213903-checkbox-selection/#findComment-1113605 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.