javascript确认取消仍然提交表单 [英] javascript confirm cancel still submits form

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

问题描述

我在表单页面上显示以下顺序,首先它通过验证码运行,然后验证电子邮件地址,然后询问您是否确定要取消订阅。



<除了单击取消仍然提交表单之外,一切都可以正常工作。我不能在提交按钮中使用onclick,因为它会绕过验证码。在我的如果电子邮件是真实的else声明我已经尝试了return和return:false,但他们都没有停止提交表单。



感谢您的帮助。

 < form action ='<?php echo $ _SERVER ['PHP_SELF']; ?>'name =unsubscribemethod ='post'onsubmit =return checkForm(this);> 

函数checkForm(form){
var frm = document。取消订阅;
if(!form.captcha.value.match(/ ^ \ d {5} $ /)){
alert('请在提供的框中输入验证码数字');
form.captcha.focus();
return false;
}
if(validEmail(frm.Email.value)!= true){
alert(Please );
frm.Email.focus();
return false;
}
if(validEmail(frm.Email.value)== true) {
confirm('您确定要取消订阅吗?');
返回true;
}
else {
返回false;
}
}

函数validEmail(email){
var status = false;
var emailRegEx = /^[A-Z0-9._%+-]+@ [A-Z0-9 .-] + \。[A- Z] {2,4} $ / i;
if(email.search(emailRegEx)== -1){
status = false;
}
else {
status = true;
}
返回状态;


解决方案

confirm 返回一个布尔值 - true 如果用户点击确定, false ,点击取消 确认调用:

  if(validEmail(frm.Email.value )== true){
return confirm('您确定要取消订阅吗?');
}


I have the following sequence on a form page, first it runs through the captcha then it validates the email address and then asks if you are sure you want to unsubscribe.

Everything works perfectly except that clicking "Cancel" still submits the form. I can't use "onclick" in the submit button because it will bypass the captcha code. In my "if the email is true 'else'" statement I've tried both "return" and "return:false" but neither of them stop the form submission.

Thanks for your help.

<form action='<?php echo $_SERVER['PHP_SELF']; ?>' name="unsubscribe" method='post' onsubmit="return checkForm(this);"">

function checkForm(form) { 
var frm = document.unsubscribe;
 if(!form.captcha.value.match(/^\d{5}$/)) {
      alert('Please enter the CAPTCHA digits in the box provided'); 
        form.captcha.focus();           
        return false;
        }   
        if (validEmail(frm.Email.value) != true) {
                alert("Please enter a valid email address");
                frm.Email.focus();
                return false;
        }   
        if (validEmail(frm.Email.value) == true) {
                confirm('Are you sure you want to unsubscribe?');  
                return true;
        }
        else {          
            return false;   
    }
}   

function validEmail(email){
    var status = false; 
    var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
    if (email.search(emailRegEx) == -1) {
    status = false;
}
    else {
    status = true;
}
    return status;  
}   

解决方案

confirm returns a boolean - true if the user clicked "Ok", false if they clicked "Cancel", so simply return the result of the confirm call:

if (validEmail(frm.Email.value) == true) {
  return confirm('Are you sure you want to unsubscribe?');
}

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

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