2011-04-12 49 views
1

我被困在了我正在處理的這個jQuery自動計算腳本的最後部分。我想根據「單選按鈕點擊」自動重新計算小計值。當單擊單選按鈕「special_(n)」時,計算會自動添加10%的折扣。基於單選按鈕的jQuery自動計算單擊

查看這裏的行動腳本:http://www.ppleasysavings.com/calcscript/index.html (TIP):輸入一個值的「數量」字段,以及總計將自動計算。

這裏是HTML「部分」

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"> 

這裏是jQuery的

正如你所看到的,我已經添加了一些代碼,包括特殊的「單選按鈕「,並已開始公式=>數量*價格*特殊。現在,我需要修改它,以便它能夠正常工作。

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

回答

0
$("input[name^=special_]").change(recalc); 

此外,似乎你的「特殊」的輸入項爲0和1的值,而不是0或0.9(爲10%)。將收音機的值設置爲這些值,然後在計算中使用它。

編輯:我一直在看它,我決定我不是你使用計算插件的粉絲。在我看來,它可以通過正常的功能實現更清潔。您進一步的循環和實際計算這樣的複雜....

1

嘗試了這一點得到單選按鈕的值進行計算:

special: $("input[name^=special_]:checked").val(), 

但mycase,只有special_1單選按鈕,就能夠使用點擊,下一個special_2不會發射任何東西,仍然可以找出原因。