admin管理员组文章数量:1404923
I have a table with some values. The first row and first column contain values of such called vertical and horizontal sizes respectfully. What I need to do is to click on a table cell and get the horizontal and vertical values for that clicked table cell. What it basically means - get the value of first table cell in the row where the clicked table cell is located and get the first table cell value of the column where the clicked table cell is located. I'm getting the horizontal value with the following code:
var horizSize = $('th:first', $(this).parents('tr')).text();
But I'm not sure how to do that for the vertical line. How to get to the first table cell value of the column?
Thanks for helping.
I have a table with some values. The first row and first column contain values of such called vertical and horizontal sizes respectfully. What I need to do is to click on a table cell and get the horizontal and vertical values for that clicked table cell. What it basically means - get the value of first table cell in the row where the clicked table cell is located and get the first table cell value of the column where the clicked table cell is located. I'm getting the horizontal value with the following code:
var horizSize = $('th:first', $(this).parents('tr')).text();
But I'm not sure how to do that for the vertical line. How to get to the first table cell value of the column?
Thanks for helping.
Share Improve this question asked Dec 29, 2011 at 12:11 cycerocycero 4,78120 gold badges59 silver badges81 bronze badges 02 Answers
Reset to default 6I think the following should work:
var verticSize = $('tr:first td').eq($(this).index()).text();
JS Fiddle demo.
Note that in the demo I've also amended your horizSize
selector, to:
horizSize = $(this).closest('tr').find('td:first').text();
And subsequently refined to:
horizSize = $(this).prevAll('td:last').text();
The above assumes that you want the value of the cell from the :first
tr
element, whether that be in the thead
or tbody
. If you've marked up the heading cells as th
then you'll need to amend the td
of the selector to th
.
Edited in response to Felix Kling's ment:
Instead of
$('tr:first td')
,$(this).closest('table').find('tr:first td')
might be better (depending on the other markup).
That's something I should've thought of, myself, really. And, if there is more than one table
in the document then this change would be essential, to demonstrate:
var verticSize = $(this).closest('table').find('tr:first td').eq($(this).index()).text();
JS Fiddle demo.
References:
closest()
.eq()
.find()
.:first
selector.index()
.:last
selector.prevAll()
.
You can navigate to the right cell with index:
$('table tr:gt(0)').each(function() {
var $valRow = $('table tr:eq(0)');
$(this).find('td:gt(0)').css( { background: "red" } ).click(function() {
var x = $valRow.find('td:eq(' + $(this).index() + ')').text(); // top row
var y = $(this).closest('tr').find('td:eq(0)').text(); // first cell of your row will be your first column cell
alert('x=' + x + '; y=' + y);
});;
});
Code: http://jsfiddle/uKhwq/1/
本文标签: javascriptGetting the first table cell value in a column with jQueryStack Overflow
版权声明:本文标题:javascript - Getting the first table cell value in a column with jQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744865068a2629297.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论