如何在jquery中获取除输入类型=“隐藏”之外的td innerhtml值

时间:2015-01-19 05:54:22

标签: javascript jquery html

我创建了一个动态表。它可以动态添加,编辑和删除行。 在表格中添加每个td时,它还会添加一个包含值的隐藏字段。就是这样,

<tr>
<td>This is Text <input type="hidden" value="someValue"></td>
</tr>

这是在td元素中获取innerHtml的代码。

var par = $(this).parent().parent(); //tr
var tdName = par.children("td:nth-child(1)");

tdName.html("<input type='text' id='txtName' value='"+tdName.html()+"'/>");

但是当我使用此代码时,它会显示带有输入隐藏类型的文本。也就是说,

This is Text <input type="hidden" value="someValue">

这里我不想得到隐藏的输入字段。我只需要This is Text的其他部分。有可能吗?

我试过tdName.children("input[type!=hidden]").val() 但它不起作用。

3 个答案:

答案 0 :(得分:3)

只需使用.text()获取该行中的文字:

tdname.children().text()

FIDDLE

答案 1 :(得分:1)

你可以试试这个,

var txt = tdname.contents().filter(function () {
    return this.nodeType == 3; //Filter it by text node
}).text();

var txt = $('.test').contents().filter(function () {
    return this.nodeType == 3; //Filter it by text node
}).text();

alert(txt);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
    <tr>
        <td class="test">This is Text
            <input type="hidden" value="someValue" />
        </td>
    </tr>
</table>

答案 2 :(得分:0)

使用:hidden选择器,它只返回可见元素:

看到这个小提琴:http://jsfiddle.net/u66ez4gv/

你需要:

tdName.children("input:visible").val()
相关问题