【发布时间】:2022-01-20 21:53:29
【问题描述】:
我正在尝试创建一个从数据库中读取数据并将其列在页面上的程序。一切正常,只是现在我在删除数据时遇到问题。
将列出数据库中的数据,并在其中添加一个删除按钮。每个按钮都具有与数据相同的 id。当我按下按钮时,带有 id 参数的函数应该使用 onclick 启动。
但我收到此错误:
index.html:1 Uncaught ReferenceError: reply_click 未定义 在 HTMLButtonElement.onclick (index.html:1:1)
我的代码:
<script type="module">
window.onload = products;
const issuesRef = ref(db, 'student');
var id = 0;
function products() {
onValue(issuesRef, (snapshot) => {
snapshot.forEach(snap => {
const issue = snap.val();
var id_2 = document.createElement("div");
var div = document.createElement("div");
var div2 = document.createElement("div");
var div3 = document.createElement("div");
var div4 = document.createElement("div");
var buttn = document.createElement("button");
buttn.setAttribute("id", issue.RollNo);
buttn.setAttribute("onclick", "reply_click(this.id)");
function reply_click(clicked_id){
console.log(clicked_id);
}
id_2.innerHTML = ++id;
div.innerHTML = issue.NameOfStd;
div2.innerHTML = issue.Gender;
div3.innerHTML = issue.RollNo;
div4.innerHTML = issue.Section;
buttn.innerHTML = "delete";
document.body.appendChild(id_2)
document.body.appendChild(div);
document.body.appendChild(div2);
document.body.appendChild(div3);
document.body.appendChild(div4);
document.body.appendChild(buttn);
})
});
}
</script>
我认为问题出在函数 reply_click() 和 buttn.setAttribute...
【问题讨论】:
标签: javascript html firebase-realtime-database