【问题标题】:how to get current input field index by jquery如何通过jquery获取当前输入字段索引
【发布时间】:2015-10-18 15:58:42
【问题描述】:

我有以下 html 表格布局:

<table id="mytable">
<tbody>
<tr>
    <td><input type="text" value="" /></td>
    <td><input type="text" value="" /></td>
    <td><input type="text" value="" /></td>
    <td>Some cell text</td>
    <td></td>
    <td><input type="text" value="" /></td>
    <td></td>
  </tr>
  <tr>
    <td><input type="text" value="" /></td>
    <td><input type="text" value="" /></td>
    <td>Some cell text</td>
    <td></td>
    <td>Some cell text</td>
    <td><input type="text" value="" /></td>
  </tr>

</tbody>
</table>

单击任何输入字段时,我需要知道相应行的当前输入字段索引,但无法找到获取它的方法。我可以通过以下公式获得每行的输入字段总数(长度):

var thisRow = $(this).closest('tr');
var inputLength = thisRow.find('input[type="text"]').length;

例如,第一行的inputLength4,第二行是3。但是当用户点击第一行的第二个输入字段(应该是 1)时,如何获取输入索引?

[注意:我不能使用td 索引来替代input 字段索引,因为您可能会看到可能有一些td 没有input 字段。结果,如果我运行for loop,它就无法按预期执行。]

【问题讨论】:

    标签: jquery html input html-table


    【解决方案1】:

    您可以使用index()方法如下:

    $(':input').on('click', function() {
      console.log($(this).closest('tr').find(':input').index(this));
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <table id="mytable">
      <tbody>
        <tr>
          <td>
            <input type="text" value="" />
          </td>
          <td>
            <input type="text" value="" />
          </td>
          <td>
            <input type="text" value="" />
          </td>
          <td>Some cell text</td>
          <td></td>
          <td>
            <input type="text" value="" />
          </td>
          <td></td>
        </tr>
        <tr>
          <td>
            <input type="text" value="" />
          </td>
          <td>
            <input type="text" value="" />
          </td>
          <td>Some cell text</td>
          <td></td>
          <td>Some cell text</td>
          <td>
            <input type="text" value="" />
          </td>
        </tr>
      </tbody>
    </table>

    另见closest()

    【讨论】:

    • 我不知道是否有更合适的解决方案,但到目前为止,这是我从你那里得到的唯一解决方案。如果今天没有其他用户能给出更合适的解决方案,我会接受你的回答。
    • 它也适用于包含data-* 的元素,例如$('[data-nav]').focus(function() {$(this).closest('tr').find('[data-nav]:input').index($(this))})
    【解决方案2】:

    尝试使用 jQuery 索引,但不要在 input 本身上,而是在 td 父级上。

    var thisRow = $(this).parent('td');
    var id = $('td').index(thisRow);
    

    上面的 id 将是 td 的索引,因此是输入的索引。

    【讨论】:

    • 我不能使用td 索引来替代input 索引。在同一行的中间可能有一些td 没有输入字段。因此,如果一行有 10 个tds 和 8 个input 字段,那么显然 td 的索引将与输入字段的索引不匹配。
    • @AbdullahMamun-Ur-Rashid 您没有在问题中提到这种情况,因为您在问题中提供的代码中的所有 td 元素实际上都有输入。
    【解决方案3】:

    请检查以下解决方案

    $('input[type="text"]').on('keypress', function(e){
        $this = $(this);   
    
        var td = $this.parents('td');
        var tr = $this.parents('tr');
    
        var col = td.parent().children().index(td);
    
        $('div').html('you are changing the value of column.#. '+col)
    
    })
    

    https://jsfiddle.net/93k7g4v7/

    【讨论】:

    • 我不能使用 td 索引作为输入索引的替代。同一行的中间可能有一些没有输入字段的 td。因此,如果一行有 10 个 tds 和 8 个输入字段,那么显然 td 的索引将与输入字段的索引不匹配。我已经用一个没有 input 字段的 td 更新了你的小提琴。现在看看指数差异。链接:jsfiddle.net/93k7g4v7/1
    【解决方案4】:

    试试index。示例:

    HTML

    <ul>
      <li id="foo">foo</li>
      <li id="bar">bar</li>
      <li id="baz">baz</li>
    </ul>
    

    JS:

    var listItem = document.getElementById("bar");
    alert("Index: " + $("li").index(listItem));
    

    【讨论】:

    • 列表项和表格行是两个不同的东西。在这里,您在用户列表 (ul) 中都有列表项 (li)。但是在表格行中,输入字段的数量可能少于 td/cell 数量。所以这两种情况下索引的识别会有所不同。
    【解决方案5】:

    尝试使用.each(function(index, element){}) 附加click 事件处理程序,在click 处理程序处引用input 元素的index

    $("input").each(function(index) {
      this.onclick = function() {
        console.log(index)
      }
    })
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <table id="mytable">
    <tbody>
    <tr><td><input type="text" value="" /></td><td><input type="text" value="" /></td><td><input type="text" value="" /></td><td>Some cell text</td><td></td><td><input type="text" value="" /></td><td></td></tr>
    <tr><td><input type="text" value="" /></td><td><input type="text" value="" /></td><td>Some cell text</td><td></td><td>Some cell text</td><td><input type="text" value="" /></td></tr>
    </tbody>
    </table>

    【讨论】:

      【解决方案6】:

      这行得通:

      <script>
      $(document).ready(function(){
          $('input[type="text"]').click(function(){
              var rowIndex = $(this).parent().parent().index('tr');
              if (rowIndex == 0) {
                  var inputIndex = $('tr:eq(0) input[type="text"]').index(this);
              } 
              else if (rowIndex == 1) {
                  var inputIndex = $('tr:eq(1) input[type="text"]').index(this);
              };
          });
      });
      </script>
      

      这是小提琴:https://jsfiddle.net/Mjollnir40/es7o8jf5/6/

      【讨论】:

      • 对不起,你的小提琴是空的(空白)!!如果我用合适的小提琴检查你的小提琴,我发现你的代码给出了所有输入字段的累积索引。例如,如果第 01 行有 5 个输入字段(有一个空白 td),则它给出最后一个输入字段的索引为 3,没关系。但是从第二行开始,第一个输入的索引从 4 开始,它不应该是。它应该再次从 0 开始。
      • 我已编辑答案以包含更改。它现在对你有用。您只需提醒 inputIndex 或将其设置为 var 或您需要的任何值。
      • 抱歉,您更新后的小提琴没有提醒或显示任何内容。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-16
      • 1970-01-01
      • 1970-01-01
      • 2017-09-12
      • 2023-03-29
      • 1970-01-01
      • 2017-04-09
      相关资源
      最近更新 更多