jQuery - 选择当前表行值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17316317/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery - Select current table row values
提问by David
When someone clicks on the Download button in my table, I want to pass the date values from that particular row to a function. Currently I can only pass the date values from the first table row.
当有人单击我表中的“下载”按钮时,我想将该特定行中的日期值传递给一个函数。目前我只能从第一个表行传递日期值。
This is the selector I'm using:
这是我正在使用的选择器:
$(this).parent().find('#period-start');
Which always returns:
总是返回:
[<td id=?"period-start">?5/1/2013?</td>]
I've tried combinations of the parent, child, find and closest selectors, but haven't been able to stumble across the correct one to grab the date values from the current row. Thanks.
我已经尝试了父、子、查找和最接近选择器的组合,但一直无法找到正确的组合来从当前行中获取日期值。谢谢。
Table
桌子
<table id="tblStatements" class="table">
<thead>
<tr>
<th>Period Starting</th>
<th>Period Ending</th>
<th>Download</th>
</tr>
</thead>
<tbody>
<tr>
<td id='period-start'>5/1/2013</td>
<td id='period-end'>5/31/2013</td>
<td><button type='submit'>Download</button></td>
</tr>
<tr>
<td id='period-start'>4/1/2013</td>
<td id='period-end'>4/30/2013</td>
<td><button type='submit'>Download</button></td>
</tr>
<tr>
<td id='period-start'>3/1/2013</td>
<td id='period-end'>3/31/2013</td>
<td><button type='submit'>Download</button></td>
</tr>
</tbody>
</table>
回答by pala?н
ID's are always supposed to be uniquein HTML. So, you might try out this, w/o using an ID:
ID在 HTML中总是应该是唯一的。所以,你可以试试这个,不使用 ID:
// Get the first td
var periodStart = $(this).closest('tr').children('td:eq(0)').text();
// Get the second td
var periodEnd = $(this).closest('tr').children('td:eq(1)').text();
回答by Mohammad Adil
Use this - don't use duplicate ID's though, use class instead
使用这个 -不要使用重复的 ID,而是使用类
$(this).closest('tr').find('#period-start');
or -
或者 -
$(this).closest('td').siblings('#period-start');
回答by bipen
try this
尝试这个
$(this).parent().siblings('#period-start');
Make sure all your ids is unique..your HTML is invalid.... change it to class and try this
确保您的所有 ID 都是唯一的...您的 HTML 无效.... 将其更改为 class 并尝试此操作
$(this).parent().siblings('.period-start');
回答by jcubic
You should not use more then one element with the same id use class insidead.
您不应该使用多个具有相同 id 的元素 use class insidead。
<tr>
<td class='period-start'>5/1/2013</td>
<td class='period-end'>5/31/2013</td>
<td><button type='submit'>Download</button></td>
</tr>
<tr>
<td class='period-start'>4/1/2013</td>
<td class='period-end'>4/30/2013</td>
<td><button type='submit'>Download</button></td>
</tr>
<tr>
<td class='period-start'>3/1/2013</td>
<td class='period-end'>3/31/2013</td>
<td><button type='submit'>Download</button></td>
</tr>
and use this code to select proper element
并使用此代码选择合适的元素
$(this).parents('tr').find('.period-start');
回答by sangram parmar
try this
尝试这个
$.trim($(this).closest('tr').find('[id="period-start"]').html());//this gives start date