英文:
How to create unique IDs when ranging over data in Golang and use in Javascript
问题
我正在从服务器获取数据,并对其进行迭代以创建一个表格,然后我使用一个表单来使用JavaScript将ID存储到本地存储中。以下是代码片段:
<table>
<tr><th>产品ID</th></tr>
{{range .}}
<td><form onsubmit="save_data()" action="/" method="get"><button class="btn btn-info pid" id="pid" name="{{.puid}}" value="{{.puid}}">更新</button></form></td>
{{end}}
<table>
<script>
function save_data() {
var input = document.getElementById("pid");
localStorage.setItem("id", input.value);
}
</script>
然而,无论我点击哪个表格行的“更新”按钮,每次都只会存储第一个表格行元素的ID。是否有办法在遍历数据时生成唯一的ID,并在JavaScript中引用它。谢谢。
英文:
I am getting data from the server and iterating over it to create a table and then I am using a form to store an id to local storage using javascript. Here is code snippet
<table>
<tr><th>Product ID</th></tr>
{{range .}}
<td ><form onsubmit="save_data()" action="/" method="get"><button class="btn btn-info pid" id="pid" name="{{.puid}}" value="{{.puid}}">Update</button></form></td>
{{end}}
<table>
<script>
function save_data() {
var input = document.getElementByID("pid");
localStorage.setItem("id", input.value);
}
</script>
However every time, no matter of which table row's "update" button I click, everytime only the ID of the first table row element is getting stored.
Is there a way I can generate unique IDs and reference it in Javascript when ranging over the data.
Thanks
答案1
得分: 1
在循环中,你有以下代码:
<button class="..." id="pid" name="{{.puid}}" value="{{.puid}}">Update</button>
这意味着所有的按钮都有相同值为pid
的id
属性。这是一个错误,因为id
在文档中必须是唯一的。当你调用
document.getElementById("pid");
时,将返回匹配id="pid"
的第一个元素。这就解释了为什么"只有第一个表格行元素的ID被存储"。
为了为每一行创建唯一的id,你可以使用以下代码:
{{range $index, $value := .}}
...<button class="..." id="pid{{$index}}" name="{{$value.puid}}" value="{{$value.puid}}">Update</button>...
{{end}}
但是,当save_data()
事件触发时,你如何知道哪个表单被提交了呢?为了解决这个问题,你可以将当前表单或行的id作为参数发送,例如:
{{range $index, $value := .}}
<td><form onsubmit="save_data(this, {{$index}})" action="/" method="get">...</form></td>
{{end}}
function save_data(form, rowno) {
var input = document.getElementById("pid"+rowno);
localStorage.setItem("id", input.value);
}
以上是翻译好的内容,请确认是否满意。
英文:
In the loop You have
<button class="..." id="pid" name="{{.puid}}" value="{{.puid}}">Update</button>
which means that all buttons have id
attribute whith the same value pid
. This is an bug as id
-s must be unique in the document. And when you call
document.getElementById("pid");
the first element matching the id="pid"
is returned. That explains why "only the ID of the first table row element is getting stored".
To create unique id for each row you could use something like
{{range $index, $value := .}}
...<button class="..." id="pid{{$index}}" name="{{$value.puid}}" value="{{$value.puid}}">Update</button>...
{{end}}
but then you have a problem how to know which form was submitted when your save_data()
event fires. To solve this you could send the current form or row id as a parameter, something like
{{range $index, $value := .}}
<td><form onsubmit="save_data(this, {{$index}})" action="/" method="get">...</form></td>
{{end}}
function save_data(form, rowno) {
var input = document.getElementById("pid"+rowno);
localStorage.setItem("id", input.value);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论