[英]generated check box will find the value of the first td in the row it is in jquery
在我的項目中,我要在td中添加一個帶復選框的td。 我會根據特定條件自動生成。 我想獲取自動生成復選框所在行的第一個td的值。
所以我想知道怎么做。 我不知道該怎么做,我沒有代碼要顯示。
我現在的想法是獲取所生成復選框所在行的索引,並將該索引用作:eq(index)
選擇器。 我仍在研究這個,因為我還沒有索引。
希望有建設性的意見
您可以獲取復選框td的父tr元素,然后獲取其第一個tr子元素。
checkbox.closest("tr").find("td").first();
closest
查找與選擇器匹配的最接近的父元素 find
在元素中搜索匹配的選擇器 first
返回第一個元素。 您也可以將其縮短為
checkbox.closest("tr").find("td:first")
Selector :first
與函數的功能相同,它匹配集合中的第一個元素。
演示版
var checkbox = jQuery("#mycheckbox"); var firstTD = checkbox.closest("tr").find("td").first(); jQuery("#log").text( "First TD: "+firstTD.text() );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="log"></div> <table> <tbody> <tr> <td>Test</td> <td>Test 2</td> <td><input type="checkbox" id="mycheckbox" /></td> <td>Test 3</td> </tr> </tbody> </table>
JSFIDDLE http://jsfiddle.net/seadonk/fbpc0jqd/
您可以使用jquery選擇器來搜索復選框,例如:
$('input[type=checkbox]')
然后,您可以使用以下命令獲取當前行中第一個td的文本:
.parents().closest('tr').children('td:first-child').text()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.