简体   繁体   English

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

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

Writing a program for an assignment and no where in my book does it help with this case. 为作业编写程序,在我的书中没有任何帮助。

After the user enters in information for a drink order form, they hit a button that reads "Order", the button calls a function that calculates and displays a confirm window with required info 用户输入饮料订购单的信息后,他们点击了显示“订购”的按钮,该按钮调用了一个函数,该函数计算并显示带有所需信息的确认窗口

If the user clicks “ok” in the confirm message, an alert message should be displayed notifying the user that the order was placed and the form should be cleared, a function is needed 如果用户在确认消息中单击“确定”,则应显示一条警告消息,通知用户已下订单并应清除该表格,需要一个功能

If the user clicks “cancel” in the confirm message, an alert message should be displayed notifying the user that the order was cancelled. 如果用户单击确认消息中的“取消”,则应显示一条警告消息,通知用户订单已取消。 The form should still contain all of the values that the user entered. 该表单仍应包含用户输入的所有值。

My issue is that I don't know what is needed to have the form reset when "OK" is selected in the confirm window. 我的问题是,当在确认窗口中选择“确定”时,我不知道重置表单需要什么。 But if "Cancel" is selected, the values remain entered. 但是,如果选择“取消”,则将保持输入值。

Here's what the code is so far 这是到目前为止的代码

Javascript (Only entering confirm portion) 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 的HTML

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

正如评论中指出的那样,您可以使用纯JavaScript并执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM