JQuery从<input />,jsfiddle之前的单元格中获取值

时间:2011-08-03 17:23:17

标签: jquery html input

我有这个HTML代码:
jsfiddle here

<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从“qty”文本框中获取值

  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,获取数量的值,获取它旁边的价格,并将其更新为div类(totalprice)仅限行。

我有几十条记录,所以这个函数应该是输入更改的特定行。

5 个答案:

答案 0 :(得分: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/

答案 1 :(得分:1)

看看这个fiddle

答案 2 :(得分:0)

如果你真的想使用jQuery,那么这应该可行:

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

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

另外,你链接的jsFiddle没有使用jQuery。

答案 3 :(得分:0)

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

和我的两分钱......使用e.srcElement来获取你的来源;)

答案 4 :(得分:0)

我认为你可以使用id选择器

来尝试
$('#qty').keyup(function() {
  alert("...");
});