【问题标题】:jquery calculation getting size of calculationsjquery计算获取计算的大小
【发布时间】:2011-04-09 04:21:30
【问题描述】:

我使用 Jquery 计算插件成功地对小计和总计执行了两次计算,就像演示页面上的最后一个示例一样:

http://www.pengoworks.com/workshop/jquery/calculation/calculation.plugin.htm

但是,我还想获得所选床位的总数,但似乎不知道如何添加它,有什么想法吗?

    function recalc(){
        //$("[id^=total_item]").calc(
        $("div[id^=subtotal_]").calc(
            // the equation to use for the calculation
            "qty * price",
            // define the variables used in the equation, these can be a jQuery object
            {
                qty: $("select[id^=room_]"),
                price: $("div[id^=price_]")
            },
            // define the formatting callback, the results of the calculation are passed to this function
            function (s){
                // return the number as a dollar amount
                return "<%=session[:symbol]%>" + s.toFixed(2);
            },
            // define the finish callback, this runs after the calculation has been complete
            function ($this){
                // sum the total of the $("[id^=total_item]") selector
                var count = $this.size(); <--- doesn't give me a correct count
                var sum = $this.sum();


                $("#totals").text(
                    // round the results to 2 digits
                    "You have selected " + count + " rooms for a total of <%=session[:symbol]%>" + sum.toFixed(2)
                );
            }
        );
    }

【问题讨论】:

  • 它有什么不妥之处?太高,太低,选择不应该的?
  • 它总是返回 4。;-) 所以我显然不明白发生了什么重要的事情。
  • 总和返回完美......这是所有小计的总和。

标签: javascript jquery jquery-plugins jquery-calculation


【解决方案1】:

没关系,这似乎有效:

    $(document).ready(
        function (){
            $("select[id^=room_]").bind("change", recalc);
        }
    );

    function recalc(){
        //$("[id^=total_item]").calc(
        var counter = $("select[id^=room_]").sum();
        $("div[id^=subtotal_]").calc(
            // the equation to use for the calculation
            "qty * price",
            // define the variables used in the equation, these can be a jQuery object
            {
                qty: $("select[id^=room_]"),
                price: $("div[id^=price_]")
            },
            // define the formatting callback, the results of the calculation are passed to this function
            function (s){
                // return the number as a dollar amount
                return "<%=session[:symbol]%> " + s.toFixed(2);
            },
            // define the finish callback, this runs after the calculation has been complete
            function ($this){
                // sum the total of the $("[id^=total_item]") selector
                var count = $this.size();
                var sum = $this.sum();

                if (sum > 0) { $("#totals").fadeIn(); } else { $("#totals").fadeOut(); }

                $("#totals").text(
                    // round the results to 2 digits
                    "You have selected " + counter + " beds for <%= user_cart.getDays %> nights totaling <%=session[:symbol]%> " + (sum * <%= user_cart.getDays.to_i %>).toFixed(2)
                );
            }
        );
    }

【讨论】:

    猜你喜欢
    • 2020-08-19
    • 1970-01-01
    • 2012-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-13
    • 1970-01-01
    相关资源
    最近更新 更多