【问题标题】:Submit Form When User Clicks OK on Confirm Box当用户在确认框上单击确定时提交表单
【发布时间】: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


【解决方案1】:

把这个放到你的函数中:

if(confirm("This is your total: " + total)) { // confirm returns true = OK clicked
    return true;
}
else { // confirm returns false = Cancel clicked
    return false;
}

【讨论】:

  • 第二个选项不起作用(在 JacobM 的回答中解释,但使用 if/else 可以。谢谢!
猜你喜欢
  • 2017-10-02
  • 2016-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-19
  • 1970-01-01
相关资源
最近更新 更多