简体   繁体   English

从兄弟元素中寻找文本

[英]finding text from sibling elements

I have dynamically generated HTML table that I want to use with JQuery. 我有动态生成的HTML表,我想用JQuery。 It looks kind of like this, only with more <tr> elements. 看起来有点像这样,只有更多的<tr>元素。

<table>
  <tr>
    <td class="name"> Apples </td>
    <td class="value"> 2.33 </td>
    <td class="amount"> <input type="text"></td>
  </tr>
</table>

The script I'm writing would take whatever's in the text input field, multiply it by the float specified in the value td and output it. 我正在编写的脚本将采用文本输入字段中的任何内容,将其乘以值td指定的浮点数并输出它。 However, I have problems getting to that <td> using JQuery. 但是,我在使用JQuery进行<td>遇到了问题。

I tried something like 我试过类似的东西

parseFloat($(".amount input").parent().siblings($("td.value")).text())

to get the value, but it doesn't work - it seems to be trying to multiply it by every element regardless of the class. 获取值,但它不起作用 - 它似乎试图将它乘以每个元素而不管类。 I tried a <tr> where the only data was integers... 我尝试了一个<tr> ,其中唯一的数据是整数...

2013-8-28 | 2.1 | 20

...and it spat out 4026. I have no idea why. ......它吐了4026.我不知道为什么。

So, the question remains: how do I get the contents of the <td> with the class value starting from the input on the same <tr> ? 所以,问题仍然存在:我如何从同一<tr>上的input开始获取带有类value<td>的内容?

Try this: 尝试这个:

$(document).ready(function(){
    $('input').on('blur', function(){
       alert($(this).val() * $(this).parent().siblings('.value').text()); 
    });
})

怎么样

$(".amount").parent().find('.value').text();

try 尝试

HTML HTML

<table>
  <tr>
    <td id="val1" class="name"> Apples </td>
    <td id="val2" class="value"> 2.33 </td>
    <td class="amount"> <input type="text"></td>
  </tr>
</table>

JavaScript JavaScript的

function getvalue()
{
  var name = (document.getElementById('val1').innerText || document.getElementById('val1').textContent);

  var value = parseFloat(document.getElementById('val2').innerText || document.getElementById('val1').textContent);   
}

you can try 你可以试试

$('.amount').each(function(){
    var $this = $(this), amount = parseFloat($this.find('input').val()) || 0, rate = parseFloat($this.prev().text()) || 0;
    console.log(amount * rate)
})

Change 更改

.siblings($("td.value"))

to

.siblings('td.value')

DEMO DEMO

This should also work 这也应该有效

$('input').on('blur', function(){
   alert($(this).val() * $(this).parent().siblings('.value').text()); 
});

Working fiddle here 在这里工作小提琴

The selector in your siblings() function is wrong. siblings()函数中的选择器是错误的。 Try this: 尝试这个:

parseFloat($(".amount input").parent().siblings(".value").text())

Example fiddle 示例小提琴

Use : 采用 :

$('input').on('blur', function(){
   var value = $(this).closest("tr").siblings(".value").text(); 
});

There are many options to do : 有很多选择:

$(".amount input").parent().prev("td.value").text() //good to use

or 要么

$(".amount input").parent().siblings("td.value").text()

or 要么

$(".amount input").parent().parent().find("td.value").text()

Full working demo 完整的工作演示

FIDDLE DEMO FIDDLE DEMO

I have done with keyup event you can however you want , FIDDLE 我已经完成了你想要的keyup事件, FIDDLE

    jQuery(function(){
        $('.amount input').keyup(function(){
            var input_value = this.value;
            var values = $(this).parent().prev().text();
            alert(input_value * values);
        })
    })

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

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