此表:
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;
// }
});
});
我想获取子密钥以便在警报中看到,原因如下:
function hola() {
var id = document.getElementById('boton').value;
alert(id);
}
但警报显示:“childkey”。我想看看:“5bm00xdQlEdIKmmYQvWU5bgtvsU2”
我该怎么做?
答案 0 :(得分:0)
当您在双引号内写childKey.innerText
时,它会充当String
而不是variable
。 childKey
本身就是文本节点,因此childKey.innerText
将是未定义的。相反,您需要使用cellId .innerText
或childKey
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;