簡體   English   中英

生成的復選框將找到它在jQuery行中的第一個td的值

[英]generated check box will find the value of the first td in the row it is in jquery

在我的項目中,我要在td中添加一個帶復選框的td。 我會根據特定條件自動生成。 我想獲取自動生成復選框所在行的第一個td的值。

所以我想知道怎么做。 我不知道該怎么做,我沒有代碼要顯示。

我現在的想法是獲取所生成復選框所在行的索引,並將該索引用作:eq(index)選擇器。 我仍在研究這個,因為我還沒有索引。

希望有建設性的意見

像這樣- 演示

$('.chk').on('click',function(){
    alert($(this).parent('td').siblings('td:first').text());
});

檢查此更新的演示 我添加了帶有和不帶有復選框的多行!

您可以獲取復選框td的父tr元素,然后獲取其第一個tr子元素。

您可以使用組合.closest.find。首先

checkbox.closest("tr").find("td").first();
  1. closest查找與選擇器匹配的最接近的父元素
  2. find在元素中搜索匹配的選擇器
  3. 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM