Javascript 当用户单击“确认”框中的“确定”时提交表单
我的HTML表单中有一个javascript,当用户单击Submit按钮时运行。它显示用户订单的总价,并具有“确定”和“取消”按钮。我希望在用户单击“确定”时提交表单,但在用户单击“取消”时不提交表单。它当前在按下任一按钮时提交表单。如果单击“取消”,如何防止其提交表单 这是我的短文:Javascript 当用户单击“确认”框中的“确定”时提交表单,javascript,html,forms,Javascript,Html,Forms,我的HTML表单中有一个javascript,当用户单击Submit按钮时运行。它显示用户订单的总价,并具有“确定”和“取消”按钮。我希望在用户单击“确定”时提交表单,但在用户单击“取消”时不提交表单。它当前在按下任一按钮时提交表单。如果单击“取消”,如何防止其提交表单 这是我的短文: <script type="text/javascript"> function calculate() { var g
<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>
函数计算()
{
var gPrice
阿普里斯变种
雪碧
var总计
gPrice=document.getElementById(“grapeorderquantity”).value*4.0;
aPrice=document.getElementById(“appleorderquantity”).value*3.0;
sPrice=document.getElementById(“strbryorderquantity”).value*3.5;
总计=(gPrice+aPrice+sPrice)*1.08;
确认(“这是您的总数:+总数);
}
这是“我的提交”按钮的HTML:
<input type="submit" value="Submit" onClick="return calculate()" />
将此应用于您的功能:
if(confirm("This is your total: " + total)) { // confirm returns true = OK clicked
return true;
}
else { // confirm returns false = Cancel clicked
return false;
}
可以显示取消按钮的HTML吗?你知道吗,显示整个表单的HTML。确定和取消按钮没有HTML。它们是在javascript中使用确认窗口时自动生成的。@Jason9024-您需要阻止
表单
元素上的提交
事件,而不是提交
按钮上的单击
。第二个选项不起作用(在对JacobM答案的评论中解释了),但使用if/else可以。谢谢!