dsklzerpx64815631 2014-05-22 12:51 采纳率: 100%
浏览 153

如何获取表格内元素的位置(col,row)?

I am studing jquery to post input tables. All inputs need to be posted with their indexes. I am guessing that i cant use ids or classes of input elements to post values with cell location info. .Because Input tables are generated dynamically according to user answer.

For Example;

User enters '4' value to a question and a 3col 4row input table is generated.

<table>
  <tr><td><input type="text"></td> <td><input type="text"></td> <td><input type="text"></td> 
  </tr>
  <tr><td><input type="text"></td> <td><input type="text"></td> <td><input type="text"></td> 
  </tr>
  <tr><td><input type="text"></td> <td><input type="text"></td> <td><input type="text"></td> 
  </tr>
  <tr><td><input type="text"></td> <td><input type="text"></td> <td><input type="text"></td> 
  </tr>
</table>

And by using jquery handler i can store the values...

$(function () {
  $("input").change(function() {
    // save this.value() in an array.
    array.push(this.value());
  });
});

I stuck at this moment because i have to store values with their (x,y)indexes in the table. Shortly; values of this 2 dimension table must be converted to a 2dim. data array in server-side.

Something like...

$(function () {
  $("input").change(function() {
    array[this.col][this.row] = this.value();
  });
});

To sum up;

Is it possible to get the location(col,row) of an element, which is inside a table?

  • 写回答

3条回答 默认 最新

  • dstbp22002 2014-05-22 13:02
    关注

    You can use index() method as follows

    $(function () {
      $("input").change(function () {
        var row = $(this).closest('tr').index(); //index of row
        var col= $(this).closest('td').index(); //index of column
        // your code
      });
    });
    

    JSFiddle Demo

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP