2011-08-03 13 views
1

我有這樣的HTML代碼:
jsfiddle hereJQuery的從小區​​在此之前​​<input>獲得價值,的jsfiddle

<table align="center"> 
    <td align="center" colspan="5">Table</td> 
    <tbody> 
     <tr> // Table Headings (Not important, removed) 
     </tr> 
      // First Row (of many)    
     <tr> 
      <td>Cell 1</td>    // Cell 1 
      <td>Cell 2</td>    // Cell 2 

      // This is the cell I want the price in 
      <td class="unitprice" align="center">2.99</td> // Cell 3 <-- I want this value 

      // Onkeyup of this input box, get the value in cell 3 into a var. 
      <td align="center"> 
      <input type="text" id="qty" name="qty" size="2" value="1" onkeyup="getValues();" onkeypress="return isNumberKey(event);"/> 
      </td> 

      <td class="totalprice" align="center">TOTAL PRICE</td> 
     </tr> 
</tbody> 
</table> 

我使用這個JavaScript來從「數量」文本框中的值,

function getValues(e) 
    { 
     var qty = e.value; // Get me the value in qty 
     // This is what I tried 
     //var Something = $(this).parent().find("td.unitprice").html(); 
     alert(Something); // Does not work 
    } 

基本上

輸入[QTY]的Onblur,獲取qty的值,獲取它旁邊的價格,並將其更新到div類(totalprice)中。

我有幾十條記錄,所以這個函數應該是針對特定行的輸入被改變的。

回答

2
$('input[type="text"]').keyup(function(){ 
    var _parent = $(this).parent(); 
    _parent.next().html(Number(_parent.prev().html()) * Number($(this).val())); 
}); 

工作演示:http://jsfiddle.net/AlienWebguy/6x7wn/9/

使用該資源:http://www.texotela.co.uk/code/jquery/numeric/

+0

這是有效的,但只適用於輸入框的第二次更改,即:如果我刷新頁面,並更改第1行輸入,它將無法正常工作,直到我第二次更改它,然後才能正常工作。即時使用它在一個功能現在。 – Anil

+0

小提琴對我來說是第一次嘗試。不應該太難以模仿邏輯。你改變了什麼? – AlienWebguy

+0

小提琴工作正常,但當我使用我的本地主機,它不工作的第一次,只有第二+點擊。我沒有改變一件事,它第一次工作,除了這個問題。我將如何在dom上加載它,所以它會運行一次(當它不會做任何事情時),並準備好在用戶更改字段時使用它。 – Anil

0

如果你真的想使用jQuery那麼這應該工作:

$('#qty').keyup(function() { 
    alert($(this).val()); 
}); 

http://jsfiddle.net/6x7wn/6/

而且,t他jsFiddle你鏈接不使用jQuery。

0

我想你可以使用id選擇

$('#qty').keyup(function() { 
    alert("..."); 
}); 
1

試試這個看看這個fiddle