我创建了一个动态表。 它可以动态添加,编辑和删除行。在表内添加每个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()
但它不起作用。
只需使用 .text()
即可获取该行中的文本:
tdname.children().text()
小提琴
你可以
试试这个,
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>
使用:hidden
选择器,它将只返回可见元素:
看到这个小提琴:http://jsfiddle.net/u66ez4gv/
你需要:
tdName.children("input:visible").val()