http://vigrius.esy.es/pikabu/ лови
<?
$arCoffes = array(
"coffe1"=>"10",
"coffe2"=>"20",
"coffe3"=>"40",
"coffe4"=>"20",
"coffe5"=>"20",
"coffe6"=>"80",
"coffe7"=>"20",
"coffe8"=>"70",
);
?>
price 10P
<br>
<?foreach($arCoffes as $key=>$coffee):?>
<input class = "coffees" type = "hidden" value = <?=json_encode($arCoffes)?>>
<?=$key?> - <?=$coffee?>
<input data-coffee = <?=$key?> class = "counter" name = "<?=$coffee?>" value = 0>
<br>
<?endforeach?>
<div class = "all">
0
</div>
<div class = "price">
0
</div>
<script>
$(function(){
counter = new counter();
counter.init();
})
var counter = function()
{
var self = this;
self.values = {};
self.all = 0;
self.init = function()
{
self.coffes = JSON.parse($(".coffees").val());
$(".counter").each(function(){
var input = $(this);
var coffee = $(this).data("coffee");
var val = $(this).val();
self.values[coffee] = val;
recount();
})
$(".counter").off("input").on("input",function(){
var input = $(this);
var coffee = $(this).data("coffee");
var val = $(this).val();
var oldVal = self.values[coffee];
if(val == "")
{
self.values[coffee] = 0;
}
else
{
self.values[coffee] = val;
}
recount(oldVal,val,coffee);
})
}
function recount(oldValue,newValue,coffee)
{
var values = self.values;
if(oldValue)
{
var minus = oldValue*self.coffes[coffee];
self.all-=minus;
self.counter-=parseInt(oldValue);
var plus = newValue*self.coffes[coffee];
self.all+=plus;
self.counter+=parseInt(newValue);
}
else
{
var keys = Object.keys(self.values);
for(var i = 0 ;i<keys.length;i++)
{
var nowKey = keys[i];
var nowVal = self.values[nowKey];
var objem = self.coffes[nowKey];
var sum = nowVal*objem;
self.all+=sum;
}
self.counter = 0;
}
$(".all").html(self.all);
console.log(self.counter);
$(".price").html(self.counter*10);
}
}
