Javascript,在确认窗口中按OK后清除表单.但如果取消确认,则保留表单数据

Javascript, clear form after press OK in confirm window. But keeps form data if confirm is cancelled

本文关键字:确认 表单 如果 取消 保留 数据 窗口 Javascript OK 清除      更新时间:2023-09-26

为作业编写程序,在我的书中,它对这种情况没有任何帮助。

在用户输入饮料订单的信息后,他们点击一个写着"订单"的按钮,该按钮调用一个函数,该函数计算并显示一个带有所需信息的确认窗口

如果用户在确认消息中单击"ok",则应显示一条警告消息,通知用户订单已下,表单应被清除,需要功能

如果用户在确认消息中单击"取消",则应显示一条警告消息,通知用户订单已取消。表单应该仍然包含用户输入的所有值。

我的问题是,当在确认窗口中选择"确定"时,我不知道需要什么来重置表单。但是,如果选择了"取消",则这些值将保持输入状态。

以下是迄今为止的代码

Javascript(仅输入确认部分)

                   if (window.confirm("Fruit: " + document.drinkList.numFruit.value + ", Total: $" + totalFruit + ".00 'n"
                     + "Water: " + document.drinkList.numWater.value + ", Total: $" + totalWater + ".00 'n"
                     + "Coffee: " + document.drinkList.numCoffee.value + ", Total: $" + totalCoffee + ".00 'n"
                     + "Coke: " + document.drinkList.numCoke.value + ", Total: $" + totalCoke + ".00 'n"
                     + "Tea: " + document.drinkList.numTea.value + ", Total: $" + totalTea + ".00 'n"
                     + "Ice: " + document.drinkList.numIce.value + "'n"
                     + "Cream: " + document.drinkList.numCream.value + "'n"
                     + "Sugar: " + document.drinkList.numSugar.value + "'n"
                     + "Pinneapple Juice: " + document.drinkList.numPinn.value + "'n"
                     + "Lemon Juice: " + document.drinkList.numLemon.value + "'n'n"
                     + "Overall Total: " + (totalCoffee + totalCoke + totalFruit + totalTea + totalWater) + ".00 'n'n"
                     + "Confirm order?"))
            window.alert("The order was submitted!");
        else
            window.alert("The order was cancelled!");

HTML

        <button type="button"value="Order" onclick="calcAll()">

正如评论中所指出的,您可以使用纯javascript和do:

document.getElementById("form").reset();