英文:
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:
$('table tbody tr td :input').attr("id",function() {
return "idprefix"+$('table tbody tr td :input').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 -->
$('table tbody tr td :input').attr("id",function() {
return "idprefix"+$('table tbody tr td :input').index($(this));
})
<!-- language: 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>
<!-- 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 = $('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);
}
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<thead>
<th>#</th>
<th>First Name</th>
</thead>
<tbody>
<tr>
<td>01</td>
<td>John</td>
<td><input type="text" /></td>
</tr>
<tr>
<td>02</td>
<td>John</td>
<td><input type="text" /></td>
</tr>
<tr>
<td>03</td>
<td>John</td>
<td><input type="text" /></td>
</tr>
</tbody>
</table>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论