首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当用户单击“确认框上的确定”时提交表单

当用户单击“确认框上的确定”时提交表单
EN

Stack Overflow用户
提问于 2014-03-10 15:57:45
回答 1查看 1.6K关注 0票数 1

我的HTML表单中有一个javascript,当用户单击Submit按钮时运行。它显示用户订单的总价格,并有"ok“和"cancel”按钮。我希望当用户单击"ok“时提交表单,但如果用户单击"cancel”则不提交。当前,当按下任一按钮时,它都会提交表单。如果单击“取消”,如何防止它提交表单?

这是我的简短剧本:

代码语言: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:

代码语言:javascript
复制
<input type="submit" value="Submit" onClick="return calculate()" />
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-10 16:00:02

把这个放在你的职能上:

代码语言:javascript
复制
if(confirm("This is your total: " + total)) { // confirm returns true = OK clicked
    return true;
}
else { // confirm returns false = Cancel clicked
    return false;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22305070

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档