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

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

问题描述

对于一个带有警告的简单表单,询问字段是否填写正确,我需要一个这样做的函数:


  • 在单击按钮时显示一个提示框,其中有两个选项:


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




我认为JavaScript确认可行,但我似乎无法弄清楚。



我现在的代码是:

 < script> 
function show_alert(){
alert(xxxxxx);
}
< / script>
< form>
< input type =imagesrc =xxxborder =0name =submitonclick =show_alert();
alt =PayPal - 更安全,更简单的网上付款方式!值= 提交 >
< / form>


解决方案

简单的内嵌JavaScript确认就足够了:

 < form onsubmit =return confirm('您确实要提交表单吗?'); > 

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

 < script> 
函数validate(form){

//验证代码在这里...


if(!valid){
alert( '请更正表单中的错误!');
返回false;
}
else {
return confirm('您确实要提交表单吗?');
}
}
< / script>
< form onsubmit =return validate(this);>


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:

    • If "OK" is clicked, the form is submitted
    • If cancel is clicked, the alert box closes and the form can be adjusted and resubmitted

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

The code I have now is:

<script>
function show_alert() {
   alert("xxxxxx");
}
</script>
<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>

解决方案

A simple inline JavaScript confirm would suffice:

<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天全站免登陆