2010-05-04 108 views
0

我有一個表,我試圖做一個定價清單,自動計算稅收組件。jQuery兄弟選擇器?

我從price_1獲取值,應用數學cal並將其保存到tax_1。我可以讀取id的結尾數字,但希望jQuery有更清晰的方式。

E.g.我將有很多領域如..

  • PRICE_1
  • price_2
  • price_3
  • tax_1
  • tax_2
  • tax_3等...

我可以使用下面的代碼在價格變化時調用jQuery,並獲得該價格的價值。我如何更新旁邊的稅務字段?我應該使用兄弟選擇器還是其他?

$('#pricing').delegate("input", "change", function(){ 

    $(this).val() /* the value of the price */; 

}) 
+0

你可以張貼一些HTML? – GerManson 2010-05-04 05:32:59

回答

0

我將需要一些HTML來給出更好的答案,但是你要求的是非常簡單的。

$(document).ready(function() { 
    $(".price").blur(function() { // when the price input loses focus 
     var price = $(this).val();  
     var tax = price * 1.15; // use whatever formula you need here, this is for Mexico. 
     $(this).next().val(tax); 
    } 
}); 

這當然假設有一個輸入顯示每個價格輸入旁邊的稅額。 如果你決定使用一個標籤或跨度只是改變$(this).next().val(tax);$(this).next().text(tax);

+0

感謝所有人都會試試明天,讓你知道我是如何去的:) – Brett 2010-05-04 11:00:06

0

如果稅收領域存在旁邊的價格像你說的,你可以使用next()以選擇。如果你也發佈你的html,會更好。

0

這取決於你的DOM結構,但通常你可以有像

var commonParent = 'tr'; // a common parent for finding related nodes 

$('#pricing').delegate("input", "change", function(){ 

    var val = $(this).val() /* the value of the price */; 
    var tax = val; 

    $(this).parents(commonParent).find('input[name=*tax]').val(tax); 
})