简体   繁体   English

在keyup上使用jquery从最近的span标签中获取文本

[英]Grab the text from the nearest span tag using jquery on keyup press

I'm trying to grab the text from the nearest span tag with the class 'item_price' and save it to a variable - can anyone tell me whats wrong 我正在尝试使用“ item_price”类从最近的span标记中获取文本并将其保存到变量中-有人能告诉我怎么了吗

Also I am trying to grab the 'hidden' input also 我也试图抓住“隐藏”的输入

    $('#ajax_basket').on('keyup','input',function(event) {    
     var qty = $(this).val();
     var item_price = $(this).find('span.item_price').text();
             var hidden_id;
     console.log(qty);
     console.log(item_price);
    });

<form id="ajax_basket">
<table>
    <tr><td><input type="hidden" name="1[rowid]" value="5333a934f53d623eb18c490b57522d93"></td></tr>
<tr>
  <td>Apple iPhone 2G</td>
  <td><input type="text" name="1[qty]" value="1" maxlength="2" size="1" class="input-mini qty"  /></td>
  <td style="text-align:right" class="item_price_row">&#36;<span class="item_price">15.00</span></td>
  <td style="text-align:right" class="sub_total">&#36;15.00</td>
</tr>
<tr>
  <td>Apple iPhone 5G</td>
  <td><input type="text" name="1[qty]" value="1" maxlength="2" size="1" class="input-mini qty"  /></td>
  <td style="text-align:right" class="item_price_row">&#36;<span class="item_price">115.00</span></td>
  <td style="text-align:right" class="sub_total">&#36;115.00</td>
</tr>   
    </tr>
</table>
</form>

You have to backstep to the parent tr , then find 您必须退后到父tr ,然后find

$('#ajax_basket').on('keyup','input',function(event) {    
    var qty = this.value
    var item_price = $(this).closest("tr").find('span.item_price').text();

    var hidden_id;
    console.log(qty);
    console.log(item_price);
});

The below line 下一行

$(this).find('span.item_price').text();

gets searches for span with class item_price among descendents of text box. 在文本框的后代之间使用class item_price搜索span

Try something like below: 尝试以下操作:

$(this).parent().parent().find('span.item_price').text();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM