另一個jQuery計算問題。jQuery將字段更改爲相關字段的子字符串
我有這個,這是從我擺弄得到這個工作的插件網站示例代碼:
function recalc(){
$("[id^=total_item]").calc(
"qty * price",
{
qty: $("input[name^=qty_item_]"),
price: $("input[name^=price_item_]"),
},
function (s){ return s.toFixed(2);},
function ($this){ var sum = $this.sum();
$("#grandTotal").val(
// round the results to 2 digits
sum.toFixed(2)
);
}
);
}
變化在價格領域造成總計更新:
$("input[name^=price_item_]").bind("keyup", recalc);
recalc();
問題是我不知道價格字段的價值,它們只會作爲用戶輸入值的子字符串提供給我,稱之爲'itemcode'。根據php查詢,會有不定數量的項目。
我想出這個來改變基礎上itemcode價格:
$("input[name^='itemcode_item_1']").keyup(function() {
var codeprice = this.value.substring(2,6);
$("input[name^='price_item_1']").val(codeprice);
});
$("input[name^='itemcode_item_2']").keyup(function() {
var codeprice = this.value.substring(2,6);
$("input[name^='price_item_2']").val(codeprice);
});
但是當它這樣做,它也更新停止ITEM_TOTAL。另外,我覺得必須有一種方法,不需要爲列表中的每個項目編寫一個編號的函數。然而,當我只使用
$("input[name^='itemcode_item_']")
更新任何項目代碼字段更新所有價格字段,這是不好的。
任何人都可以指向正確的方向嗎?我知道我在這裏有點無知,但任何類型的JavaScript都不是我的東西。
好的,現在更新這個來添加一些html。基本上,現在這只是jquery計算器網站上的一小部分示例,我正在使用它來在將所有這些功能全部放入實際運行的腳本之前獲得功能。實際上會有不定數量的項目,並且會以各種方式進行分組。
<table>
<tr><th>Qty</th><th align="left"></th><th>Item code</th><th>Price</th><th>Total</th></tr>
<tr>
<td><input type="text" name="qty_item_1" id="qty_item_1" value="1" size="3"/></td>
<td>Item One</td>
<td><input type="text" id="itemcode_item_1" name="itemcode_item_1" value="" size="8" /></td>
<td><input type="text" id="price_item_1" name="price_item_1" value="" size="8" /></td>
<td><input type="text" id="total_item_1" value=" " size="8" readonly="readonly" /></td>
</tr>
<tr>
<td><input type="text" name="qty_item_2" id="qty_item_2" value="1" size="3"/></td>
<td>Item Two</td>
<td><input type="text" id="itemcode_item_2" name="itemcode_item_2" value="" size="8" /></td>
<td><input type="text" id="price_item_2" name="price_item_2" value="" size="8" /></td>
<td><input type="text" id="total_item_2" value=" " size="8" readonly="readonly" /></td>
</tr>
<tr>
<td colspan="4" align="right"><strong>Grand Total:</strong></td>
<td><input type="text" id="grandTotal" value=" " size="6" readonly="readonly"> </td>
</tr>
</table>
如果您可以提供一些示例HTML,那將會很好。也許我們可以找出另一個jQuery選擇器組合。另外,爲什麼它會停止重新計算功能?我是對的,你的基本問題是,從其他值(作爲子串)更新價格值並重新計算總和?但說實話,我覺得很奇怪,用戶可以輸入一些包含價格的「itemcode」。這是真的嗎?作爲用戶,我可以輸入任何我喜歡的價格,或者我錯了嗎? – 2010-04-01 00:52:20
這不是一個公共頁面,只有一個人會使用它。它採用條形碼掃描器的條形碼。 – Katherine 2010-04-01 01:02:04
好的。這說明了它:) – 2010-04-01 01:02:59