2010-04-01 83 views
1

另一個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> 
+0

如果您可以提供一些示例HTML,那將會很好。也許我們可以找出另一個jQuery選擇器組合。另外,爲什麼它會停止重新計算功能?我是對的,你的基本問題是,從其他值(作爲子串)更新價格值並重新計算總和?但說實話,我覺得很奇怪,用戶可以輸入一些包含價格的「itemcode」。這是真的嗎?作爲用戶,我可以輸入任何我喜歡的價格,或者我錯了嗎? – 2010-04-01 00:52:20

+0

這不是一個公共頁面,只有一個人會使用它。它採用條形碼掃描器的條形碼。 – Katherine 2010-04-01 01:02:04

+0

好的。這說明了它:) – 2010-04-01 01:02:59

回答

1

你寫這三元素都以不同的方式進行分組,但如果這種模式始終保持(即相關領域是一個行內):

<tr> 
    <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> 
</tr> 

你可以得到你的功能是這樣的:

$("input[name^='itemcode_item_']").keyup(function() { 
    var codeprice = this.value.substring(2,6); 
    // closest(sel) finds the closest ancestor that matches 'sel' 
    // find(sel) finds all descendants that match the selector 'sel' 
    $(this).closest('tr').find("input[name^='price_item_']").val(codeprice); 
}); 

一般情況下,嘗試將itemcode_item_領域及其相應的price_item_場之間找出維吾爾語。
在這種情況下,它們有一個共同的祖先tr,並且其中只有price_item_輸入字段。另一種關係是,該price_item_1itemcode_item_1父母的下一個兄弟姐妹的第一個孩子。

+0

好天才,聰明!非常感謝你,它像一個魅力。是啊! – Katherine 2010-04-01 01:59:38

+0

@凱瑟琳:)我的榮幸。 – 2010-04-01 02:05:28

1

我已經解決了第二個問題(總計不更新)這一點,我應該想到前:

 $("input[name^=itemcode_item_]").bind("keyup", recalc); 
     recalc(); 

現在只是如何做到這一點,而無需重複功能,每個編號項目。

+0

正如我在我的評論中說,如果你顯示一些示例HTML代碼,我們可能會找出一些方法。但它取決於結構;) – 2010-04-01 01:00:42