【发布时间】:2014-03-10 15:57:45
【问题描述】:
我的 HTML 表单中有一个 javascript,当用户单击“提交”按钮时运行。它显示用户订单的总价,并具有“确定”和“取消”按钮。我希望在用户单击“确定”时提交表单,但如果用户单击“取消”则不提交。它当前在按下任一按钮时提交表单。如果单击“取消”,如何阻止它提交表单?
这是我的简短脚本:
<script type="text/javascript">
function calculate()
{
var gPrice
var aPrice
var sPrice
var total
gPrice = document.getElementById("grapeorderquantity").value * 4.0;
aPrice = document.getElementById("appleorderquantity").value * 3.0;
sPrice = document.getElementById("strbryorderquantity").value * 3.5;
total = (gPrice + aPrice + sPrice) * 1.08;
confirm("This is your total: " + total);
}
</script>
这是我提交按钮的 HTML:
<input type="submit" value="Submit" onClick="return calculate()" />
【问题讨论】:
-
你能显示取消按钮的 HTML 吗?你知道吗,显示整个表单的 HTML。
-
OK 和 CANCEL 按钮没有 HTML。它们是在 javascript 中使用确认窗口时自动生成的。
-
@Jason9024 - 您需要阻止
form元素上的submit事件,而不是submit按钮上的click。
标签: javascript html forms