简体   繁体   English

如何将数据分配给Firebase数据库提取的输入标签中的value属性?

[英]How to assign data to value attribute in input tag, pulled by firebase database?

I used the code snippet below to pull data from Firebase database and assign it to the Name variable. 我使用下面的代码片段从Firebase数据库中提取数据并将其分配给Name变量。

var userId = localStorage.getItem('keyName');

var dbRefName = firebase.database().ref()
                                   .child('Web App')
                                   .child('Users')
                                   .child(userId)
                                   .child('Name');

dbRefName.on('value', snap => Name.innerText = snap.val());

var Name = document.getElementById('Name');

Then, I try to assign the Name variable to value attribute of the input tag like this: 然后,我尝试将Name变量分配给input标签的value属性,如下所示:

<input type="text"
       id="Name"
       class="form-control form-control-alternative"
       placeholder="User Name"
       value="Name">

But it is not working, could anyone help me? 但这不起作用,有人可以帮助我吗?

add this line var Name = document.getElementById('Name'); document.getElementById("Name").value = Name; 添加此行var Name = document.getElementById('Name'); document.getElementById("Name").value = Name; var Name = document.getElementById('Name'); document.getElementById("Name").value = Name; and remove value="Name" from your html 并从您的html中删除value="Name"

I found the answer by my self. 我自己找到了答案。 experience the excellence. 体验卓越。 Here it is 这里是

var userId = localStorage.getItem('keyName');

    var dbRefName = firebase.database().ref().child('Web App').child('Users').child(userId).child('Name');

     dbRefName.on("value", function(snapshot) {
      console.log(snapshot.val());

      document.getElementById('Name').value = snapshot.val();

    }, function (errorObject) {
      console.log("The read failed: " + errorObject.code);
    });

And the input tag should be looked like here. 输入标签应如下所示。

<input type="text"
       id="Name"
       class="form-control form-control-alternative"
       placeholder="User Name"
       >

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM