javascript - 对话框关闭时如何清除对话框内的表格

标签 javascript html

单击按钮时,将添加 2 组数据。我使用 Material 设计。

第一次需要点击两次按钮才能运行功能。因此,数据被添加到表中两次。

代码

HTML

<button onclick="purchaseList(orderid)" id="dialog">Button</button>

JS

function popup(listid) {

    var starCountRef = firebase.database().ref('Orders/' +
        listid).child('foodItems');
    starCountRef.on('child_added', snapshot => {

        var snaps = snapshot.val();
        var itemPrice = snaps.price;
        var itemName = snaps.productName;
        var itemQuantity = snaps.quantity;

        console.log(itemName);
        $("#producttable").append(
            '<tr><td class="mdl-data-table__cell--non-numeric">' + itemName +
            '</td><td>' + itemQuantity + '</td><td>' + itemPrice + '</td></tr>'
        );
    });

    var dialog = document.querySelector('dialog');
    var showDialogButton = document.querySelector('#dialog');
    if (!dialog.showModal) {
        dialogPolyfill.registerDialog(dialog);
    }
    showDialogButton.addEventListener('click', function() {


        dialog.showModal();

    });
    dialog.querySelector('.close').addEventListener('click', function() {

        var element = document.getElementById("producttable")
        while (element.lastChild) {
            element.removeChild(element.lastChild);
        }
        dialog.close();

    });

}

最佳答案

这应该有效:

var element = document.getElementById("producttable")
while (element.lastChild) {
    element.removeChild(element.lastChild);
}

根据需要添加此内容。

关于javascript - 对话框关闭时如何清除对话框内的表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49707676/

相关文章:

javascript - 如何以屏幕阅读器仍会阅读文本并向用户指示它已被禁用的方式将单选按钮设置为 "disable"?

html - 我的 HTML/CSS 网站末尾的空白

JQuery:使用两个数组创建链接列表

javascript - 用于跳转/单击的 jQuery/jQuery UI slider 事件。函数未触发

javascript - 在javascript中连接以访问全局预定义的变量?

javascript - 将变量传递给 JavaScript

javascript - HTML5 数据库 - 事务 VS executeSql 回调顺序

javascript - 展开折叠问题

javascript - 按钮 onclick 打开 WordPress 短代码

带有回车符和换行符的javascript错误