从表中获取文本

时间:2017-11-26 17:41:20

标签: html

此表:

var tblusuario = document.getElementById('tbl_usuario_list');
    var databaseRef = firebase.database().ref('Usuarios/');
    var rowindex = 1;

    databaseRef.once('value', function(snapshot) {
        snapshot.forEach(function(childSnapshot) {

            var childKey = childSnapshot.key;
            var childData = childSnapshot.val();

            var row = tblusuario.insertRow(rowindex);
            var cellId = row.insertCell(0);
            var cellboton = row.insertCell(1);


             cellId.appendChild(document.createTextNode(childKey));
             cellboton.innerHTML = '<button class="btn btn-primary btn-xs my-xs-btn" type="button" id="boton" value="childKey.innerText" onclick="hola()">'
                                      + '<span class="glyphicon glyphicon-pencil"></span> Eliminar</button>';


                rowindex = rowindex + 1;
           // }

        });
    });

table

我想获取子密钥以便在警报中看到,原因如下:

 function hola() {

         var id = document.getElementById('boton').value;
         alert(id);
      }

但警报显示:“childkey”。我想看看:“5bm00xdQlEdIKmmYQvWU5bgtvsU2”

我该怎么做?

1 个答案:

答案 0 :(得分:0)

当您在双引号内写childKey.innerText时,它会充当String而不是variablechildKey本身就是文本节点,因此childKey.innerText将是未定义的。相反,您需要使用cellId .innerTextchildKey

&#13;
&#13;
var childKey = "ABSDBASDBSADDSA";
var tblusuario = document.getElementById('tbl_usuario_list');
var row = tblusuario.insertRow(0);
var cellId = row.insertCell(0);
var cellboton = row.insertCell(1);


cellId.appendChild(document.createTextNode(childKey));
cellboton.innerHTML = '<button class="btn btn-primary btn-xs my-xs-btn" type="button" id="boton" value="' + childKey + '" onclick="hola()">' +
  '<span class="glyphicon glyphicon-pencil"></span> Eliminar</button>';

function hola() {
  var id = document.getElementById('boton').value;
  alert(id);
}
&#13;
<table id="tbl_usuario_list"></table>
&#13;
&#13;
&#13;