2012-04-05 154 views
0

我對jQuery很新,需要根據用戶放置在2個文本框中的內容來創建計算。JQuery計算錯誤:「未捕獲的類型錯誤:對象[對象對象]沒有方法'值'」

我的形式如下:

<tr> 
     <td with='110'>&pound; $ProductCostEach</td> 
     <td with='110'>Per tile</td> 
     <td width='55'> 
      <input type='hidden' name='pid' id='pid' value='$ProductID' /> 
      <input name='quantity' class='quantity' type='text' size='1' maxlength='4' value='1' style='width:40px;' /> 
     </td> 
     <td width='110'> 
      <button type='submit' class='red'><span class='buy'>Add to basket</span></button> 
     </td> 
</tr><tr> 
<td with='110'>&pound; $ProductCostPerMeter</td> 
<td with='110'>Per Meter $TileSize</td> 
<td width='55'> 
      <input type='hidden' name='pid' id='pid' value='$ProductID' /> 
      <input name='msq' class='msq' type='text' size='1' maxlength='4' value='1' style='width:40px;' /> 
     </td> 
     <td width='110'> 
      <button type='submit' class='red'><span class='buy'>Add to basket</span></button> 
     </td> 
</tr> 

現在,當用戶輸入與一類.msq我希望它進行計算的輸入框的值,所以寫了一個keyup事件:

function roundNumber(num, dec) { 
     var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec); 
     return result; 
} 

var convMultiplier 
convMultiplier = 0.0625 

$('.msq').bind('keyup', function(){ 
     var qty_text_box = $(this).parent().parent().parent().find('.quantity'); 

     qty_text_box.value(roundNumber($(this).value * convMultiplier, 2)); 
}); 

但有一個問題剛剛得到輸入框的值。如果我在var qty_text_box = $(this).parent()。parent()。parent()。find('。quantity');後添加alert(qty_text_box.value())我得到一個錯誤「遺漏的類型錯誤:對象的翻譯:有沒有方法‘價值’」

但我在努力工作,出了什麼問題?

提前許多感謝,

大連

回答

0

似乎不像你的父母()的字符串jquery調用正在找到正確的元素。如果您在同一頁面上沒有多個數量輸入,您應該只給它一個ID並以此方式引用它。

如果您在頁面上有多個數量輸入,您應該將被點擊的按鈕和父元素中的輸入(可能位於同一行)分組,以使事情更容易,然後通過類似$(this ).parents('。parentEl')。find('。quantity')

或者,您可以選擇使用$(this).closest('。quantity')來獲取輸入,但不會看到更多HTML我不能告訴你,如果這將可靠地工作。

編輯也是你的第二次使用。值需要parens(.value())

相關問題