我有一个firebase数据库,我想获取所有聊天记录,并在其中添加一个HTML元素.每个div都有一个密码输入和一个按钮.我创建了一个forEach()循环,但它只为最后一个循环创建一个事件侦听器.我不是很有经验,所以这可能是一个简单的问题.这是我的代码:

get(ref(db, "chats")).then((snapshot)=>{

    
    snapshot.forEach((child)=>{
        var html = "";
        var childName = child.val().chatname;
        var childPassword = child.val().chatpassword;
        console.log(childName);
        html += `
        <li>
            <div class="chat-login-container">
            <h3>`+childName+`</h3>
            <input id="`+childName+`-input" type="password" placeholder="Enter Chat Password">
            <button id="`+childName+`-button" class="login">Enter</button>
            </div>
        </li>
        `;
        messages.innerHTML += html;
        var button = document.getElementById(childName+"-button");
        var passwordInput = document.getElementById(childName+"-input");
        button.addEventListener("click", ()=>{
            get(ref(db, "chats/"+childName)).then((snapshot) =>{
                if(passwordInput==childPassword){
                    chat = childName;
                    console.log("Logging in to "+childName);
                    messages.innerHTML = "";
                    start();
                }else{
                    window.alert("Incorrect password, please try again");
                }
            }).catch((error)=>{
                console.log(error);
            });
        });
    });
    }).catch((error)=>{
        console.log(error);
    });   

推荐答案

因为,您正在按ID捕获按钮.它将捕获记录的最后一个ID.

Instead use querySelector.

请try 下面的代码,这不是正确的代码,您需要根据错误消息进行调整.

get(ref(db, "chats")).then((snapshot) => {


    snapshot.forEach((child) => {
        var html = "";
        var childName = child.val().chatname;
        var childPassword = child.val().chatpassword;
        console.log(childName);
        html += `
        <li>
            <div class="chat-login-container">
            <h3>`+ childName + `</h3>
            <input type="hidden" class="childname" value="`+ childname + `">
            <input type="hidden" class="childPassword" value="`+ childPassword + `">
            <input id="`+ childName + `-input" class="password" type="password" placeholder="Enter Chat Password">
            <button id="`+ childName + `-button" class="login">Enter</button>
            </div>
        </li>
        `;
        messages.innerHTML += html;

    });
    const chatLoginContainers = document.querySelectorAll('.chat-login-container');
    chatLoginContainers.forEach((element) => {
        const loginBtn = element.querySelector('.login');
        loginBtn.addEventListener('click', (event) => {
            const childNameEle = event.target.closest('li').querySelector('.childname');
            const childPasswordEle = event.target.closest('li').querySelector('.childPassword');
            const passwordEle = event.target.closest('li').querySelector('.password');
            const childNameVal = childNameEle.value;
            const childPasswordVal = childPasswordEle.value;
            const passwordVal = passwordEle.value;
            get(ref(db, "chats/" + childNameVal)).then((snapshot) => {
                if (passwordVal == childPasswordVal) {
                    chat = childNameVal;
                    console.log("Logging in to " + childNameVal);
                    messages.innerHTML = "";
                    start();
                } else {
                    window.alert("Incorrect password, please try again");
                }
            }).catch((error) => {
                console.log(error);
            });
        })
    });

}).catch((error) => {
    console.log(error);
});   

Javascript相关问答推荐

序列查找器功能应用默认值而不是读取响应

有什么(最佳)方法可以从模块中获取脚本模块的多姆元素吗?

根据总价格对航班优惠数组进行排序并检索前五个结果- Angular HTTP请求

未捕获错误:在注销后重定向到/login页面时找不到匹配的路由

按下同意按钮与 puppeteer 师

InDesign—创建一个独立的窗口,在文档中进行更正时保持打开状态

使用i18next在React中不重新加载翻译动态数据的问题

给定一个凸多边形作为一组边,如何根据到最近边的距离填充里面的区域

函数返回与输入对象具有相同键的对象

Prisma具有至少一个值的多对多关系

如何在coCos2d-x中更正此错误

如何使本地html页面在重新加载时保持当前可隐藏部分的打开状态?

ngOnChanges仅在第二次调用时才触发

是否可以在不更改组件标识的情况下换出Reaction组件定义(以维护状态/引用等)?如果是这样的话,是如何做到的呢?

我怎样才能得到一个数组的名字在另一个数组?

如何使用[ModelJSON,ArrayBuffer]调用tf.loadGraphModelSync

用Reaction-RT-Chart创建实时条形图

如何压缩图像并将其编码为文本?

TabNavigator和StackNavigator之间的Reaction Native中的导航问题

在不使用AJAX的情况下将JavaScript数组值传递给Laravel控制器?