Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery—RecreacteTotal—单击时添加和减去价格事件_Javascript_Jquery - Fatal编程技术网

Javascript jQuery—RecreacteTotal—单击时添加和减去价格事件

Javascript jQuery—RecreacteTotal—单击时添加和减去价格事件,javascript,jquery,Javascript,Jquery,我是jQuery的新手,所以我通过试用,寻求更有经验的帮助 这是代码的一部分 click: function () { //Click event if (this.status() == 'available') { //optional seat var maxSeats = 3; var ms = sc.find('selected').length; //alert(ms); if (ms < maxSeats) {

我是jQuery的新手,所以我通过试用,寻求更有经验的帮助

这是代码的一部分

 click: function () { //Click event
  if (this.status() == 'available') { //optional seat
   var maxSeats = 3;
   var ms = sc.find('selected').length;
   //alert(ms);
   if (ms < maxSeats) {

                price = this.settings.data.price;

                /*
                    $('<option selected>R'+(this.settings.row+1)+' S'+this.settings.label+'</option>')
                    .attr('id', 'cart-item-'+this.settings.id)
                    .attr('value', (this.settings.row+1)+'_'+this.settings.label)
                    .data('seatId', this.settings.id)
                    .appendTo($cart);
                */

                    $('<option selected>R'+(this.settings.row+1)+' S'+this.settings.label+'</option>')
                    .attr('id', 'cart-item-'+this.settings.id)
                    .attr('value', this.settings.id)
                    .attr('alt', price)
                    .data('seatId', this.settings.id)
                    .appendTo($cart);

                $counter.text(sc.find('selected').length+1);
                $counter.attr('value', sc.find('selected').length+1);

                $total.text(recalculateTotal(sc));
                $total.attr('value', recalculateTotal(sc));

                return 'selected';
            }
                alert('You can only choose '+ maxSeats + ' seats.');
                return 'available';

        } else if (this.status() == 'selected') { //Checked

                //Update Number
                $counter.text(sc.find('selected').length-1);
                $counter.attr('value', sc.find('selected').length-1);
                //update totalnum
                $total.text(recalculateTotal(sc));
                $total.attr('value', recalculateTotal(sc));

                //Delete reservation
                $('#cart-item-'+this.settings.id).remove();
                //optional
                return 'available';

        } else if (this.status() == 'unavailable') { //sold
            return 'unavailable';

        } else {
            return this.style();
        }
    }
});
实际上,此函数计算每次单击地图的总价格,并在再次单击项目(取消选择)时重新计算。 当我单击以选择某个项目时,函数会正确计算总数;当我再次单击某个项目以取消选择该项目时,在第一次取消选择(或单击)中,不会减去金额

为了更具体,请举一个例子: 如果我选择三个项目,每个项目的价格为10欧元,则每次单击都会正确更新总数(10+10+10=30); 当我取消选择三个元素时,当我第一次单击时,总数不会更新,而在接下来的单击(取消选择)中,总数会更新,因此它不会返回=0,而是10

因此,我发现没有选择任何元素,但总共需要10个元素

我试图将这一变化包括在内,但计算完全错误

...........
...........
                $total.text(recalculateTotal(sc)+this.settings.data.price);
                $total.attr('value', recalculateTotal(sc)+this.settings.data.price);
                return 'selected';

            }

                alert('You can only choose '+ maxSeats + ' seats.');
                return 'available';

        } else if (this.status() == 'selected') { //Checked
......
......
                //update totalnum
                $total.text(recalculateTotal(sc)-this.settings.data.price);
                $total.attr('value', recalculateTotal(sc)-this.settings.data.price);
如果您有任何建议,我们将不胜感激

往上走

            //Delete reservation
            $('#cart-item-'+this.settings.id).remove();
以前

            //update totalnum
            $total.text(recalculateTotal(sc));
            $total.attr('value', recalculateTotal(sc));
这里是完整正确的代码

    click: function () { //Click event
  if (this.status() == 'available') { //optional seat
   var maxSeats = 3;
   var ms = sc.find('selected').length;
   //alert(ms);
   if (ms < maxSeats) {

                price = this.settings.data.price;

                /*
                    $('<option selected>R'+(this.settings.row+1)+' S'+this.settings.label+'</option>')
                    .attr('id', 'cart-item-'+this.settings.id)
                    .attr('value', (this.settings.row+1)+'_'+this.settings.label)
                    .data('seatId', this.settings.id)
                    .appendTo($cart);
                */

                    $('<option selected>R'+(this.settings.row+1)+' S'+this.settings.label+'</option>')
                    .attr('id', 'cart-item-'+this.settings.id)
                    .attr('value', this.settings.id)
                    .attr('alt', price)
                    .data('seatId', this.settings.id)
                    .appendTo($cart);

                $counter.text(sc.find('selected').length+1);
                $counter.attr('value', sc.find('selected').length+1);

                $total.text(recalculateTotal(sc));
                $total.attr('value', recalculateTotal(sc));

                return 'selected';
            }
                alert('You can only choose '+ maxSeats + ' seats.');
                return 'available';

        } else if (this.status() == 'selected') { //Checked

                //Update Number
                $counter.text(sc.find('selected').length-1);
                $counter.attr('value', sc.find('selected').length-1);

                //Delete reservation
                $('#cart-item-'+this.settings.id).remove();

                //update totalnum
                $total.text(recalculateTotal(sc));
                $total.attr('value', recalculateTotal(sc));


                //optional
                return 'available';

        } else if (this.status() == 'unavailable') { //sold
            return 'unavailable';

        } else {
            return this.style();
        }
    }
});
click:function(){//click事件
如果(this.status()=='available'){//可选座位
var maxSeats=3;
var ms=sc.find(‘选定’)。长度;
//警报(毫秒);
如果(毫秒<最大座位){
价格=this.settings.data.price;
/*
$('R'+(this.settings.row+1)+'S'+this.settings.label+“”)
.attr('id','cart item-'+this.settings.id)
.attr('value',(this.settings.row+1)+''.'+this.settings.label)
.data('seatId',this.settings.id)
.appendTo($cart);
*/
$('R'+(this.settings.row+1)+'S'+this.settings.label+“”)
.attr('id','cart item-'+this.settings.id)
.attr('value',this.settings.id)
.attr('alt',价格)
.data('seatId',this.settings.id)
.appendTo($cart);
$counter.text(sc.find('selected')。长度+1);
$counter.attr('value',sc.find('selected')。长度+1);
$total.text(重新计算总计(sc));
$total.attr('值',重新计算总计(sc));
返回“选定”;
}
警报('您只能选择'+maxSeats+'seats');
返回“可用”;
}如果(this.status()=='selected'){//已选中,则为else
//更新编号
$counter.text(sc.find('selected')。长度为1);
$counter.attr('value',sc.find('selected')。长度-1);
//删除预订
$(“#购物车项目-”+this.settings.id).remove();
//更新totalnum
$total.text(重新计算总计(sc));
$total.attr('值',重新计算总计(sc));
//可选的
返回“可用”;
}如果(this.status()='unavailable'){//sall
返回“不可用”;
}否则{
返回此.style();
}
}
});

(不太确定dupe,因为问题中似乎缺少相关的HTML代码。但即使它不是复选框,而是选择字段,我认为问题是相同的-
在值/选择发生实际更改之前单击
激发;
更改
是在这种情况下需要的事件。)如果您查看此
$counter.text(sc.find('selected')。长度+1)$计数器.attr('value',sc.find('selected')。长度+1)
和这个`$counter.text(sc.find('selected')。长度为1)$counter.attr('value',sc.find('selected')。长度-1);`你会发现计数器的解是加+1和减-1。所以,找一些类似的东西,不知道你的意思。@CBroe解决方案
    click: function () { //Click event
  if (this.status() == 'available') { //optional seat
   var maxSeats = 3;
   var ms = sc.find('selected').length;
   //alert(ms);
   if (ms < maxSeats) {

                price = this.settings.data.price;

                /*
                    $('<option selected>R'+(this.settings.row+1)+' S'+this.settings.label+'</option>')
                    .attr('id', 'cart-item-'+this.settings.id)
                    .attr('value', (this.settings.row+1)+'_'+this.settings.label)
                    .data('seatId', this.settings.id)
                    .appendTo($cart);
                */

                    $('<option selected>R'+(this.settings.row+1)+' S'+this.settings.label+'</option>')
                    .attr('id', 'cart-item-'+this.settings.id)
                    .attr('value', this.settings.id)
                    .attr('alt', price)
                    .data('seatId', this.settings.id)
                    .appendTo($cart);

                $counter.text(sc.find('selected').length+1);
                $counter.attr('value', sc.find('selected').length+1);

                $total.text(recalculateTotal(sc));
                $total.attr('value', recalculateTotal(sc));

                return 'selected';
            }
                alert('You can only choose '+ maxSeats + ' seats.');
                return 'available';

        } else if (this.status() == 'selected') { //Checked

                //Update Number
                $counter.text(sc.find('selected').length-1);
                $counter.attr('value', sc.find('selected').length-1);

                //Delete reservation
                $('#cart-item-'+this.settings.id).remove();

                //update totalnum
                $total.text(recalculateTotal(sc));
                $total.attr('value', recalculateTotal(sc));


                //optional
                return 'available';

        } else if (this.status() == 'unavailable') { //sold
            return 'unavailable';

        } else {
            return this.style();
        }
    }
});