我创建了一个 readAll() 函数,它正在将数据提取到 todos div 中,但它没有在点击函数上显示数据,但它不工作数据正在索引数据库中成功添加,但它没有显示。现在我首先清空表格,然后将数据从 javascript 追加到 html 中。
function readAll() {
var objectStore = db.transaction("todostore").objectStore("todostore");
$("#todos").empty();
objectStore.openCursor().onsuccess = function(event) {
var cursor = event.target.result;
if (cursor) {
$("#todos").append("<tr><td>" + cursor.key + "</td><td>" + cursor.value.todo + "</td><td><button onclick=\"update('" + cursor.key + "','" + cursor.value.todo + "')\">Update</button></td><td><button onclick=\"removetodo('" + cursor.key + "','" + cursor.value.todo + "')\">Delete</button></td></tr>");
cursor.continue();
}
};
};
现在我在点击时调用函数 我只是调用 readAll 函数
$(".clickme").click(function(){
readAll();
});
我在 div 中创建了一个 id View 的 div,我现在使用了一个表头,我只是附加 JavaScript 中的代码并填充到 html 中
<div id="view">
<thead>
<tr class="ui-bar-d">
<th data-priority="2">Timestamp</th>
<th data-priority="1">To Do</th>
<th data-priority="3">Update</th>
<th data-priority="4">Delete</th>
</tr>
</thead>
<tbody id="todos">
</tbody>
</table>
</div>
<label for="todo">To Do:</label>
<input id="todo" type="text">
<input id="toprice" type="text">
<button class="clickme">clickme</button>
<button class="ui-shadow ui-btn ui-corner-all" onclick="add()">Add</button>
最佳答案
由于 IDB 是异步的。您需要调用您的函数......
transaction.oncomplete = function(event) {
$(".clickme").click(function(){
readAll();
});
};
https://developer.mozilla.org/en-US/docs/Web/API/IDBTransaction/oncomplete
关于javascript - 如何从 indexeddb 中检索数据并显示在 div 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41045952/