最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get element inside a td using Row index and td index - Stack Overflow

programmeradmin4浏览0评论

I have a Row Index and TD index in a table and I want to select input element inside the cell in [Row Index,TD Index] . How I can do this?

I have a Row Index and TD index in a table and I want to select input element inside the cell in [Row Index,TD Index] . How I can do this?

Share Improve this question asked Oct 30, 2012 at 11:02 DooDooDooDoo 13.5k70 gold badges189 silver badges317 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 9

Tables have accessor properties intended for direct access to individual cells, i.e.:

table.rows[rowIndex].cells[colIndex]

hence:

table.rows[rowIndex].cells[colIndex].getElementsByTagName('input')[0];

or:

$('input', table.rows[rowIndex].cells[colIndex])

This should work:

$('tr:eq(rowIndex) td:eq(tdIndex) input')

:eq selector for more information.

var rowIndex = X;
var cellIndex = Y;
$('#my-table tbody')
    .children(':nth-child('+(rowIndex+1)+')')
      .children(':nth-child('+(cellIndex+1)+')')
        .find('input').val('Hello');

of course you can put em all a single selector

$('#my-table tbody tr:nth-child('+(rowIndex+1)+') td:nth-child('+(cellIndex+1)+')')
        .find('input').val('Hello');
发布评论

评论列表(0)

  1. 暂无评论