JQuery:计算插件不计算总价格

时间:2012-02-24 14:32:39

标签: javascript jquery jquery-plugins

:插件 主页:Link

:JavaScript的

  $("[id^=total_price_ht]").calc(
    // the equation to use for the calculation
    "qty * price",
    {
      qty: $("[id^=unit_quantity_]"),
      price: $("[id^=unit_price_ht_]")
    },
    function (s){
      // return the number as a dollar amount
      return "$" + s.toFixed(2);
    }
  );

:HTML

<tr id="lines[0]">
  <td>
    <input id="0" type="checkbox" class="hiddenCheckbox">
    <label for="0" class="prettyCheckbox checkbox list"><span class="holderWrap" style="width: 18px; height: 19px; "><span class="holder" style="width: 18px; "></span></span></label>
  </td>
  <td>
    <input class="required" name="lines[0][title]" placeholder="Title" type="text">
  </td>
  <td>
    <input name="lines[0][description]" placeholder="Description" type="text">
  </td>
  <td>
    <input class="required" id="unit_quantity_0" name="lines[0][quantity]" placeholder="Quantité" type="text" value="0,00">
  </td>
  <td>
    <input class="required" id="unit_price_ht_0" name="lines[0][unit_price_ht]" placeholder="Prix unit. HT" type="text" value="0,00">
  </td>
  <td class="price" id="total_price_ht_0">$0.00</td>
</tr>

页面加载后,我可以在total_price_ht字段中看到“$ 0.00”,但是当我更改数量或价格时,它的值不会改变。

我做错了什么?感谢。

1 个答案:

答案 0 :(得分:0)

我想你每次想要重新计算总数时都需要调用calc插件:

$("[id^='unit_quantity_'], [id^='unit_price_ht_']").keyup(recalc);


function recalc() {

    $("[id^='total_price_ht']").calc(
    // the equation to use for the calculation
    "qty * price", {
        bind: "keyup",
        qty: $("[id^='unit_quantity_']"),
        price: $("[id^='unit_price_ht_']")
    }, function(s) {
        // return the number as a dollar amount
        return "$" + s.toFixed(2);
    });
}

recalc();

示例: http://jsfiddle.net/upEZW/