/ / ustawienie wartości atrybutu id jako zmiennej w html - javascript, html

ustawienie wartości atrybutu id jako zmiennej w html - javascript, html

Jak dodać atrybut id elementu w HTML jako zmiennej, która jest zadeklarowana powyżej w javascript.

jQuery

var table_row = $("table").find("tr#"+pid);
var name = table_row.find("td:nth-child(1)").html();
table_row.find("td:nth-child(6)").html("<button  type="button" id="what to write here" class ="save_db")>save</button> ");

Chcę ustawić id jako nazwę.

Dzięki

Odpowiedzi:

1 dla odpowiedzi № 1

Można to zrobić za pomocą prostego łączenia ciągów w następujący sposób:

var table_row = $("table").find("tr#"+pid);
var name = table_row.find("td:nth-child(1)").html();
table_row.find("td:nth-child(6)")
.html("<button  type="button" id=""+name+"" class ="save_db">save</button> ");

PS: Zwróć uwagę, że twoje marże zawierały pozornie niepotrzebne closing brace po class atrybut. Usunąłem go w moim kodzie.


0 dla odpowiedzi nr 2

Po prostu chcesz wstawić zmienną do łańcucha html?

table_row.find("td:nth-child(6)").html`(
"<button  type="button" id="" + name + "" class ="save_db">save</button> "
)`;

0 dla odpowiedzi № 3

Powiązać name zmienna na ciąg dodający jako ID.

table_row.find("td:nth-child(6)").html("<button  type="button" id=""+ name + ""
class ="save_db")>save</button>");

0 dla odpowiedzi nr 4

Proszę postępować zgodnie z tym kodem:

table_row.setAttribute("id", "id_you_like");

0 dla odpowiedzi № 5

Spróbuj z poniższym fragmentem kodu. Daj mi znać, jeśli nie rozumiem twojego pytania.

var id = "btn1";
var table_row = $("table").find("tr#"+pid);
var name = table_row.find("td:nth-child(1)").html();
table_row.find("td:nth-child(6)").html("<button  type="button" id=""+id+"" class ="save_db")>save</button> ");

0 dla odpowiedzi № 6

Czy próbowałeś tego? Spróbuj użyć tego:

<button type="button" id=""+name+"" class= "save_db">