开发者

JQuery get value from the cell <td> before this <td><input>, jsfiddle

I have this html code:

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>           开发者_JS百科   // 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>

I am using this javascript to get the value from the "qty" text box,

  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
  }

Basically

Onblur of input[QTY], get the value of qty, get the price next to it, and update it into the div class (totalprice) FOR THAT ROW ONLY.

I have dozens of records so this function should be for that specific row the input was changed for.


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

Working demo: http://jsfiddle.net/AlienWebguy/6x7wn/9/

Uses this resource: http://www.texotela.co.uk/code/jquery/numeric/


Take a look at this fiddle.


if you really want to use jQuery then this should work:

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

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

Also, the jsFiddle you linked was not using jQuery.


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

and my two cents... use the e.srcElement to get where you came from ;)


I think you can try this using the id selector

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜