JavaScript 表单提交 - 确认或取消提交对话框 [英] JavaScript Form Submit - Confirm or Cancel Submission Dialog Box

查看:43
本文介绍了JavaScript 表单提交 - 确认或取消提交对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于带有询问字段是否填写正确的警报的简单表单,我需要一个执行此操作的函数:

For a simple form with an alert that asks if fields were filled out correctly, I need a function that does this:

  • 点击按钮时显示一个警告框,有两个选项:

  • Shows an alert box when button is clicked with two options:

  • 如果单击确定",则提交表单
  • 如果点击取消,警告框关闭,表单可以调整并重新提交

我认为 JavaScript 确认会起作用,但我似乎不知道怎么做.

I think a JavaScript confirm would work but I can't seem to figure out how.

我现在的代码是:

function show_alert() {
  alert("xxxxxx");
}

<form>
  <input type="image" src="xxx" border="0" name="submit" onclick="show_alert();" alt="PayPal - The safer, easier way to pay online!" value="Submit">
</form>

推荐答案

一个简单的内联 JavaScript 确认就足够了:

<form onsubmit="return confirm('Do you really want to submit the form?');">

不需要外部函数,除非您正在进行验证,您可以这样做:

No need for an external function unless you are doing validation, which you can do something like this:

<script>
function validate(form) {

    // validation code here ...


    if(!valid) {
        alert('Please correct the errors in the form!');
        return false;
    }
    else {
        return confirm('Do you really want to submit the form?');
    }
}
</script>
<form onsubmit="return validate(this);">

这篇关于JavaScript 表单提交 - 确认或取消提交对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆