【问题标题】:jQuery -- recalculateTotal - add and subtract Price on click event [duplicate]jQuery - recalculateTotal - 点击事件的加减价格[重复]
【发布时间】:2017-08-04 18:35:12
【问题描述】:

我是 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();
        }
    }
});

这个问题特别是部分与总数的重新计算有关

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

在实践中,此函数计算地图上每次点击的总价格,并在再次点击项目(取消选择)时重新计算它。 当我单击选择一个项目时,该函数会正确计算总数,当我再次单击一个项目以取消选择它时,在第一次取消选择(或单击)时,不会减去金额。

更具体的举个例子: 如果我以每件 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);

任何建议将不胜感激

【问题讨论】:

  • (不太确定这个骗局,因为问题中似乎缺少相关的 HTML 代码。但即使不是复选框,而是选择字段,我认为问题是一样的 - click在值/选择的实际更改发生之前触发;change 是您在这种情况下想要的事件。)
  • 如果你看这个$counter.text(sc.find('selected').length+1); $counter.attr('value', sc.find('selected').length+1);和这个`$counter.text(sc.find('selected').length-1); $counter.attr('value', sc.find('selected').length-1);` 你会发现对于 counter 的解决方案是加 +1 和减 -1。所以找到这样的东西
  • 不知道你的意思。
  • @CBroe 解决方案

标签: javascript jquery


【解决方案1】:

这是解决方案

向上移动

            //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();
        }
    }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-04
    • 2013-04-21
    • 2013-06-18
    • 2019-02-28
    • 2013-06-20
    • 2017-11-14
    • 1970-01-01
    相关资源
    最近更新 更多