[英]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()
但它不起作用。
你可以試試看
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.