如何使用jQuery分配和循环tr号码作为id?

huangapple go评论55阅读模式
英文:

How do I assign, and loop, the tr number as id using jQuery?

问题

var currentIndex = $('table tbody tr').length;

function generateId() {
    for (i = 0; i < $('table tbody tr').length; i++) {
        console.log('Hello World', + i - 1);
        $('table tbody tr:eq(' + i + ') td :input').attr('id', + i).attr('name', + i).prop('class', 'form-control form-control-sm form-control-solid formInput-' + i);
    }
}
英文:

I want to calculate the table rows, and then assign to each <tr> in the <table> an id number based on the number of that row within the <table>.

I did the calculation like this:

var currentIndex = $('table tbody tr').length

and I got the number of rows; then, when I tried to loop through the rows, it changed the id in all the rows to the same id.

For example, I need to make it so that, for row number 1 the id is 1, for row 2 the id is 2, etc.

function generateId() {

    for (i = 0; i < $('table tbody tr').length; i++) {
        console.log('Hello World', + i -1);
        $('table  tbody tr td :input').attr('id', + i).attr('name', + i).prop('class', 'form-control form-control-sm form-control-solid formInput-' + i);
        //$('table tbody tr td :input').attr('name', + i);
        //$('#table tbody tr td :input').attr('class', 'form-control form-control-sm form-control-solid' + i);
    }

any ideas ??!!

答案1

得分: 0

你可以这样做:

$('table tbody tr td :input').attr("id", function() {
  return "idprefix" + $('table tbody tr td :input').index($(this));
});

另外,不建议将id设置为数字。

演示

<!-- 开始代码片段: js 隐藏: true 控制台: true babel: false -->

<!-- 语言: lang-js -->

$('table tbody tr td :input').attr("id", function() {
  return "idprefix" + $('table tbody tr td :input').index($(this));
})

<!-- 语言: lang-html -->

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tbody>
    <tr>
      <td>
        <input />
      </td>
    </tr>
    <tr>
      <td>
        <input />
      </td>
    </tr>
    <tr>
      <td>
        <input />
      </td>
    </tr>
    <tr>
      <td>
        <input />
      </td>
    </tr>
  </tbody>
</table>

<!-- 结束代码片段 -->
英文:

You can do it like this:

$(&#39;table  tbody tr td :input&#39;).attr(&quot;id&quot;,function() {
  return &quot;idprefix&quot;+$(&#39;table  tbody tr td :input&#39;).index($(this));
});

Also it's not recommended that id is a number.

Demo

<!-- begin snippet: js hide: true console: true babel: false -->

<!-- language: lang-js -->

$(&#39;table  tbody tr td :input&#39;).attr(&quot;id&quot;,function() {
  return &quot;idprefix&quot;+$(&#39;table  tbody tr td :input&#39;).index($(this));
})

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;table&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;
        &lt;input /&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;
        &lt;input /&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;
        &lt;input /&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;
        &lt;input /&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

<!-- end snippet -->

答案2

得分: 0

根据行数为每一行分配id,你可以这样做:-

const rows = $('table tbody tr');
for (i = 0; i < rows.length; i++) {
   let row = $(rows)[i];
   let input = $(row).children('td').children('input');
   $(row).attr('id', i+1);
   $(input).attr('id', i+1);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
    <thead>
      <th>#</th>
      <th>名字</th>
    </thead>
    <tbody>
      <tr>
        <td>01</td>
        <td>约翰</td>
        <td><input type="text" /></td>
      </tr>
      <tr>
        <td>02</td>
        <td>约翰</td>
        <td><input type="text" /></td>
      </tr>
      <tr>
        <td>03</td>
        <td>约翰</td>
        <td><input type="text" /></td>
      </tr>
    </tbody>
  </table>
英文:

To give id to each row based on number of rows. You can do it like this:-

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const rows = $(&#39;table tbody tr&#39;);
for (i = 0; i &lt; rows.length; i++) {
   let row = $(rows)[i];
   let input = $(row).children(&#39;td&#39;).children(&#39;input&#39;);
   $(row).attr(&#39;id&#39;, i+1);
   $(input).attr(&#39;id&#39;, i+1);
}

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;table&gt;
    &lt;thead&gt;
      &lt;th&gt;#&lt;/th&gt;
      &lt;th&gt;First Name&lt;/th&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
      &lt;tr&gt;
        &lt;td&gt;01&lt;/td&gt;
        &lt;td&gt;John&lt;/td&gt;
        &lt;td&gt;&lt;input type=&quot;text&quot; /&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;02&lt;/td&gt;
        &lt;td&gt;John&lt;/td&gt;
        &lt;td&gt;&lt;input type=&quot;text&quot; /&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;03&lt;/td&gt;
        &lt;td&gt;John&lt;/td&gt;
        &lt;td&gt;&lt;input type=&quot;text&quot; /&gt;&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月14日 03:51:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76244613.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定