[英]How do I get specific data from my localstorage so I can show them in a modal in JavaScript?
for example, let's say I have:例如,假设我有:
student[ id: 1, Name: Jake]学生[身份证:1,姓名:杰克]
student[ id: 2, Name: John]学生[身份证:2,姓名:约翰]
so far what I have is到目前为止我所拥有的是
const link = document.createElement("a");
link.href = "#";
link.addEventListener("click", function(){openModalCallback()}, false);
link.textContent = student.id;
let row = document.createElement("tr");
let cell = document.createElement("td");
row.appendChild(cell);
cell.append(link);
this just opens the modal that contains the data.这只是打开包含数据的模式。 How do I specifically access the 2nd student when I click on his ID so only his data will be shown in the modal?当我点击第二个学生的 ID 时,我如何专门访问他的 ID,以便只有他的数据会显示在模式中?
You could do this a number of ways.你可以通过多种方式做到这一点。 I like to add data-id='x' to the anchor element.我喜欢将 data-id='x' 添加到锚元素。 Then when the anchor is clicked get that data id and use it in the lookup.然后,当单击锚点时,获取该数据 ID 并在查找中使用它。
so:所以:
<a id="personSelect" href="#" data-id="1">John Smith</a>
and you have an object:你有一个 object:
var student = {
1:Jake,
2:John
}
You can get the data-id with:您可以通过以下方式获取数据 ID:
const person = document.getElementById('personSelect');
const id = person.dataset.id;
Then you can access the person然后你可以访问这个人
student[id]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.