无法将数组对象从映射函数传递为参数JavaScript
我试图弄清为什么我不能将数组对象从映射函数作为参数传递到函数createModal(student)
被称为onClick:
该错误是由参数引起的,因为当我尝试函数createModal()
时,它效果很好。
const allWorkshops = await fetch("/api/workshops/detail");
const data = await allWorkshops.json();
result.innerHTML = "";
counter = 0;
data.forEach((workshop) => {
...
${workshop.students.map(student =>
`<li class="list-group-item">
<a data-bs-toggle="modal" data-bs-target="#studentModal" type="button" onclick="createModal(${student})">${student.first_name} ${student.last_name}</a>
</li>`
).join("")}
...
});
我已经看过 this /Question/122102/What-Is-the-the-the-the-the-way-to-clone-an-ar-object-in-javascript“> this )问题,并尝试更改代码,但没有以下内容也可以工作:
...
${workshop.students.map(function(student) {
var studentCopy = JSON.parse(JSON.stringify(student));
return `<li class="list-group-item">
<a data-bs-toggle="modal" data-bs-target="#studentModal" type="button" onclick="createModal(${studentCopy})">${student.first_name} ${student.last_name}</a>
</li>`
}).join("")}
...
不幸的是,我不知道确切的错误,因为我在Chrome中唯一看到的是此错误。这是在索引文件的开始,对我无济于事。
uckulting语法:意外的标识符(在(索引):1:21)
编辑: 我已经更新了代码以显示以下内容。它有点有效,但问题是有一个用于研讨会的表,每个行都有一个带有学生的列表,而不是将学生附加到正确的研讨会上,而是将它们附加到第一个列表中,而另一个是空的。
const workshopList = document.querySelector(".workshop-list");
async function getData(){
const allWorkshops = await fetch("/api/workshops/detail");
const data = await allWorkshops.json();
data.forEach((workshop) => {
var studentList = document.querySelector(".student-list");
const tr = document.createElement("tr");
tr.classList.add("responsive-table__row");
tr.innerHTML = `
<td class="responsive-table__body__text responsive-table__body__text--name">
<ul class="list-group student-list">
${workshop.students.forEach(student => {
let li = document.createElement('li');
li.classList.add('list-group-item');
let anchor = document.createElement('a');
anchor.dataset.bsToggle = 'modal';
anchor.dataset.bsTarget = '#studentModal';
anchor.setAttribute('type', 'button');
anchor.addEventListener('click', () => createModal(student));
anchor.innerText = student.last_name;
li.appendChild(anchor);
studentList.appendChild(li);
})}
</ul>
</td>
`
workshopList.appendChild(tr);
});
};
I am trying to figure out why I cannot pass array object from map function as argument to function createModal(student)
that is called onclick:
The error is caused by the parameter because when I tried function createModal()
it worked fine.
const allWorkshops = await fetch("/api/workshops/detail");
const data = await allWorkshops.json();
result.innerHTML = "";
counter = 0;
data.forEach((workshop) => {
...
${workshop.students.map(student =>
`<li class="list-group-item">
<a data-bs-toggle="modal" data-bs-target="#studentModal" type="button" onclick="createModal(${student})">${student.first_name} ${student.last_name}</a>
</li>`
).join("")}
...
});
I have seen this (and this) question and tried changing the code as following but it did not work either:
...
${workshop.students.map(function(student) {
var studentCopy = JSON.parse(JSON.stringify(student));
return `<li class="list-group-item">
<a data-bs-toggle="modal" data-bs-target="#studentModal" type="button" onclick="createModal(${studentCopy})">${student.first_name} ${student.last_name}</a>
</li>`
}).join("")}
...
Unfortunately I don't know the exact error because the only thing I see in inspect in Chrome is this. Which is at the beggining of index file and does not help me.
Uncaught SyntaxError: Unexpected identifier (at (index):1:21)
EDIT:
I have updated the code to look as following. It kinda works but the problem is that is have a table for workshops and every row has a list with the students inside and instead of appending students to the right workshop it appends them to the first list other are empty.
const workshopList = document.querySelector(".workshop-list");
async function getData(){
const allWorkshops = await fetch("/api/workshops/detail");
const data = await allWorkshops.json();
data.forEach((workshop) => {
var studentList = document.querySelector(".student-list");
const tr = document.createElement("tr");
tr.classList.add("responsive-table__row");
tr.innerHTML = `
<td class="responsive-table__body__text responsive-table__body__text--name">
<ul class="list-group student-list">
${workshop.students.forEach(student => {
let li = document.createElement('li');
li.classList.add('list-group-item');
let anchor = document.createElement('a');
anchor.dataset.bsToggle = 'modal';
anchor.dataset.bsTarget = '#studentModal';
anchor.setAttribute('type', 'button');
anchor.addEventListener('click', () => createModal(student));
anchor.innerText = student.last_name;
li.appendChild(anchor);
studentList.appendChild(li);
})}
</ul>
</td>
`
workshopList.appendChild(tr);
});
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Student
是一个对象。当您尝试将其插入模板文字中时,它将变成字符串[Object Object]
,这不是对create_modal()
的有意义的参数。您应该使用DOM方法创建元素,而不是插值,并使用
addeventListener()
使用闭合来绑定单击侦听器。student
is an object. When you try to interpolate it into the template literal, it gets turned into the string[Object object]
, which isn't a meaningful argument tocreate_modal()
.Instead of interpolating, you should create the element using DOM methods, and use
addEventListener()
with a closure to bind the click listener.