我怎样才能获得td第一个孩子的价值?

时间:2014-02-04 11:49:09

标签: javascript jquery html

我有一张我想要的表格,对于每一行,我都会得到第一个td,对于这个td,我想要第一个input子值。

我能够获得输入的标记,但我无法提取值。

这是我的代码:

$('#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);
        })
    });

我的提醒输出:

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

但我想要的只是价值..我尝试过.text(),。val(),但它没有用......

6 个答案:

答案 0 :(得分:3)

尝试这个并将它们全部作为数组获取一次:

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

答案 1 :(得分:1)

尝试

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

答案 2 :(得分:1)

您不需要使用两个循环来查找第一个孩子,您也必须使用.val()来获取输入的值,试试这个

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

答案 3 :(得分:1)

您需要使用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);
    })
});

答案 4 :(得分:0)

使用.val()获取输入元素的值

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

答案 5 :(得分:0)

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,不关心声誉。 2如果不起作用,请使其正常工作。

相关问题