简体   繁体   English

我如何获得第一个孩子的价值?

[英]How can I get td first child value?

I have a table where I want, for each row, get the first td and for this td I want the first input child value. 我有一个表,我想在每一行中获取第一个td ,为此td我想要第一个input子值。

I'm able to get the markup for the input, but I can't extract the value. 我可以获取输入的标记,但是无法提取值。

Here is my code: 这是我的代码:

$('#table-unidades tr:not(:last-child)').each(function () {
        $('td:first-child', this).each(function () {
            var firstCell = $(this).children().eq(0);
            var text = firstCell.html();
            alert(text);
        })
    });

my alert outputs this: 我的警报输出以下内容:

<input type="text" name="unidades-first-td" value="UN - Unidade" disabled />

But all I want is the value.. I've tried with .text(), .val() , but it didn't work... 但是我想要的只是值。.我尝试使用.text(),.val(),但是没有用...

try this and get them all as an array once: 尝试一下,将它们全部作为一个数组获取:

$("#table-unidades tr:not(:last-child)>td:first-child input:text:first-child")
                  .map(function () {return $(this).val(); }).get();

Try 尝试

var val = firstCell.find('input').val();
alert(val);

You don't need to use two each loops to find first child, also you must use .val() to get the value of an input, try this 您不需要使用两个循环来查找第一个孩子,也必须使用.val()来获取输入值,请尝试以下操作

$('#table-unidades tr:not(:last-child)').each(function () {
    var text = $(this).find('td:first input:first').val();
});

You need to use val() to get value of input, then use find() to find input field inside td 您需要使用val()获取输入值,然后使用find()td内查找输入字段

$('#table-unidades tr:not(:last-child)').each(function() {
    $('td:first-child', this).each(function() {
        var firstCell = $(this).children().eq(0);
        var text = firstCell.find('input').val();
        alert(text);
    })
});

Use .val() to get the value of the input element 使用.val()获取输入元素的值

$('#table-unidades tr:not(:last-child) td:first-child').each(function () {
    var firstCell = $(this).children('input');
    var text = firstCell.val();
    alert(text);
});
var trs = $("tr", ".my-table");
for(var i=0; i<trs.length; i++){
    var nval = $("td:first-child > input", trs[i]).val();
    console.log(nval);
}

1 don't care about the reputation. 1不在乎声誉。 2 if it doesn't work, make it work. 2如果它不起作用,请使其起作用。

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

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