/ / Come aggiungere l'attributo id con il numero incrementato in Jquery o Javascript - javascript, jquery, loop, attributi

Come aggiungere l'attributo id con il numero incrementato in Jquery o Javascript - javascript, jquery, loop, attributi

Ho cercato a lungo di impostare un idattributo all'elemento riga tabella della tabella. Ogni volta che termina il ciclo, finisco per avere l'unico id = "9" su tutto il tag. c'è un modo per incrementare l'attributo id da 0 a 9, cioè il primo tag ottiene id = "0" e l'ultimo ottiene id = "8".

$(function() {
$("form").children().last().on("click", makeGrid)

function makeGrid(e) {
e.preventDefault();
var height = $("#input_height").val();
var width = $("#input_width").val();
var color = $("#colorPicker").val();
var pxheight = height + "px";
var pxwidth = width + "px";

for (var i = 0; i < height; i++) {
$("table").append("<tr></tr>");
$("tr").attr("id", i);
}
$("table").css({
height: pxheight,
width: pxwidth
})
}
})

risposte:

0 per risposta № 1

Basta selezionare quell'elemento:

for(var i = 0; i < height; i++)
$("table").append( $("<tr></tr>").attr("id", i) );