简体   繁体   English

如何从本地存储中获取特定数据,以便在 JavaScript 中以模态显示它们?

[英]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.

相关问题 Alpha 任何地方:如何从列表中获取所有数据,以便我可以使用 Javascript 访问它们 - Alpha anywhere: How to get all the data from the list so that I can access them using Javascript 如何将 append 元素添加到我的引导模式以显示来自本地存储的数据? - How do I append elements to my bootstrap modal to display data from localstorage? 如何将数据行ID从格式化程序转移到模态下,以便我可以处理适当的记录 - How do I transfer the data-row-id from my formatter to my modal so I can process the proper record 如何从Paypal取回数据,以便可以相应地更改MySQL数据库? - How do I get data back from Paypal so I can alter my MySQL database accordingly? 如何从输入中获取值并在表格模式中显示它们? - how can i get values from input and show them in table modal? 如何从 localstorage 获取项目并将其呈现到我的列表中? - How do i get Items from localstorage and render it to my list? 如何从 localStorage 获取项目并在我的 UI 中显示? - How do I get items from localStorage and display in my UI? 尝试在 localstorage 中获取对象的索引,以便我可以使用 javascript 或 jquery 从 localStorage 存储中删除/删除该对象 - Trying to get index of object in localstorage so I can delete/remove that object from localStorage storage, using javascript or jquery 如何从本地存储中获取数据并使用 javascript 在表中查看数据并进行下拉列表对表进行排序 - How can i get the data from localstorage and view in table using javascript and make a dropdown to sort the table 我如何将数据从 javascript 插入到 php 表中,或者如何使用数据库而不是使用 localStorage 执行以下任务? - how do i insert data into a php table from javascript or How can i do the following task using database instead of using localStorage?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM