Javascript jquery计算获取计算的大小

Javascript jquery计算获取计算的大小,javascript,jquery,jquery-plugins,jquery-calculation,Javascript,Jquery,Jquery Plugins,Jquery Calculation,我使用Jquery计算插件成功地对小计和总计执行了两次计算,与演示页面上的最后一个示例非常相似: 然而,我也想得到一个被选中的床位总数,但似乎不知道如何添加它,有什么想法吗 function recalc(){ //$("[id^=total_item]").calc( $("div[id^=subtotal_]").calc( // the equation to use for the calculation

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

然而,我也想得到一个被选中的床位总数,但似乎不知道如何添加它,有什么想法吗

    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)
                );
            }
        );
    }
函数recalc(){
//$(“[id^=total_item]”)。计算(
$(“div[id^=小计]”)。计算(
//用于计算的方程式
“数量*价格”,
//定义方程式中使用的变量,这些变量可以是jQuery对象
{
数量:$(“选择[id^=房间]”),
价格:$(“div[id^=price_]”)
},
//定义格式化回调,计算结果将传递到此函数
职能{
//将数字作为美元金额返回
返回“+s.toFixed(2);
},
//定义finish回调,它在计算完成后运行
功能($this){
//求$(“[id^=total\u item]”选择器的总和

var count=$this.size();不管怎样,这似乎是可行的:

    $(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)
                );
            }
        );
    }
$(文档)。准备好了吗(
函数(){
$(“select[id^=room_u3;]”).bind(“change”,recalc);
}
);
函数recalc(){
//$(“[id^=total_item]”)。计算(
变量计数器=$(“选择[id^=room_uz]”).sum();
$(“div[id^=小计]”)。计算(
//用于计算的方程式
“数量*价格”,
//定义方程式中使用的变量,这些变量可以是jQuery对象
{
数量:$(“选择[id^=房间]”),
价格:$(“div[id^=price_]”)
},
//定义格式化回调,计算结果将传递到此函数
职能{
//将数字作为美元金额返回
返回“+s.toFixed(2);
},
//定义finish回调,它在计算完成后运行
功能($this){
//求$(“[id^=total\u item]”选择器的总和
var count=$this.size();
var sum=$this.sum();
如果(总和>0){$(“#总计”).fadeIn();}其他{$(“#总计”).fadeOut()}
$(“#总计”).text(
//将结果四舍五入到2位数
“您已选择了“+柜台+”床位,总计”+(总和*)。toFixed(2)
);
}
);
}

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