dragon4808 2011-04-12 15:59
浏览 58

基于Radio Button Click的jQuery Autocalculate

I am stuck on the last part of this jQuery auto-calculation script that I am working on. I want to automatically re-calculate the subtotal values based on a "radio button click". The calculation will automatically add a 10% discount when the radio button "special_(n)" gets clicked.

View the script in action here: http://www.ppleasysavings.com/calcscript/index.html (tip): enter a value into the 'qty' field, and the totals will auto calculate.

Here is the HTML 'part'

Yes<input name="special_1" type="radio" value="1" />
No<input name="special_1" type="radio" value="0" />
<input name="total_item_1" type="text" id="total_item_1" style="text-align:right;" value="$0.00" size="7" maxlength="8" readonly="readonly">

Yes<input name="special_2" type="radio" value="1" />
No<input name="special_2" type="radio" value="0" />
<input name="total_item_2" type="text" id="total_item_2" style="text-align:right;" value="$0.00" size="7" maxlength="8" readonly="readonly">

Here is the jQuery

As you can see, I have already added a bit of code to include the special "radio button", and have started on the equation => qty * price * special. Now, I need to modify it so that it works.

var bIsFirebugReady = (!!window.console && !!window.console.log);
$(document).ready(
    function (){
        // update the plug-in version
        $("#idPluginVersion").text($.Calculation.version);

    // bind the recalc function to the quantity fields
        $("input[name^=qty_item_]").bind("keyup", recalc);
        $("input[name^=special_]").bind("checked", recalc);
        // run the calculation function now
        recalc();
    }
);  
function recalc(){
    $("[id^=total_item]").calc(
        // the equation to use for the calculation
        "qty * price * special",
        // define the variables used in the equation, these can be a jQuery object
        {
            qty: $("input[name^=qty_item_]"),
            price: $("[id^=price_item_]"),
            special: $("input[name^=special_]")
        },
        // define the formatting callback, the results of the calculation are passed to this function
        function (s){
            // return the number as a dollar amount
            return "$" + 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 sum = $this.sum();

            $("#grandTotal").val(
                // round the results to 2 digits
                "$" + sum.toFixed(2)
            );
        }
    );
}
  • 写回答

2条回答 默认 最新

  • doushih06137 2011-04-12 16:06
    关注
    $("input[name^=special_]").change(recalc);
    

    In addition, it seems that your 'special' input item has values of 0 and 1, instead of 0 or .9 (for the 10% off). Set the value of the radio to those values and then use it in your calculation.

    EDIT: I kept looking at it and I've decided I'm not a fan of your use of the calculate plugin. Seems to me that it could be accomplished much cleaner with normal functionality. You further complicate your looping and actual calculation like this....

    评论

报告相同问题?

悬赏问题

  • ¥15 我想在一个软件里添加一个优惠弹窗,应该怎么写代码
  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流