繁体   English   中英

如何在jQuery中获取除input type =“ hidden”之外的td innerhtml值

[英]How to get td innerhtml values except input type=“hidden” in jquery

我创建了一个动态表。 它可以动态地添加,编辑和删除行。 在表中添加每个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()

暂无
暂无

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

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