简体   繁体   English

(Javascript)如何删除警报框后的“未定义”

[英](Javascript) How do i remove the 'undefined' after alert box

/* Validating Captcha Function */
function ValidCaptcha() {
    var str1 = removeSpaces(document.getElementById('txtCaptcha').value);
    var str2 = removeSpaces(document.getElementById('txtCompare').value);

    if (str1 == str2) {
        alert("請按OKAY兩次 (Please Press OKAY Twice To Submit)");
        document.getElementById('contact-form').action = "/send_form_email.php";
    }
    else {
        alert("請輸入正確的號碼 (Please Enter The Correct Vertification Number)");
        event.preventDefault();
        event.stopPropagation();
    }
}

This is the code i use for a javascript captcha element on my website, this code works perfectly, however after the alert boxes pops up, an 'Undefined' will always follow. 这是我在我的网站上用于javascript验证码元素的代码,该代码可以完美运行,但是在弹出警告框后,始终会出现“未定义”。 I have already tried return true and return false with no luck. 我已经尝试过没有运气就返回true和false。 MANY THANKS <3 非常感谢<3

BTW this is how i initiate the javascript form onsubmit in HTML 顺便说一句,这就是我如何启动HTML中的javascript表单onsubmit的方法

<form id="contact-form" class="form-horizontal"  method="post" onsubmit="return alert(ValidCaptcha());">

When you are calling submit 致电时提交

onsubmit="return alert(ValidCaptcha());"

You have an alert. 您有一个警报。 Your ValidCaptcha does not return anything, hence why it is undefined. 您的ValidCaptcha不返回任何内容,因此为什么未定义。 Also alert returns nothing so makes no sense. 警报也不会返回任何内容,因此毫无意义。

Should just be 应该只是

onsubmit="return ValidCaptcha();"

and now you should be returning a boolean. 现在您应该返回一个布尔值。

function ValidCaptcha() {
  var str1 = removeSpaces(document.getElementById('txtCaptcha').value);
  var str2 = removeSpaces(document.getElementById('txtCompare').value);

  if (str1 == str2) {
    alert("請按OKAY兩次 (Please Press OKAY Twice To Submit)"); 
    document.getElementById('contact-form').action = "/send_form_email.php";
    return true
  } else {
    alert("請輸入正確的號碼 (Please Enter The Correct Verification Number)");  //<Vertification should be  Verification
    event.preventDefault();
    event.stopPropagation();
    return false;
  }
}

onsubmit was wrong function call change like this onsubmit="return ValidCaptcha(event);" onsubmit是错误的函数调用更改,例如onsubmit="return ValidCaptcha(event);" .And also pass the event with onsubmit .Then only e.preventDeafult() is working 然后还通过onsubmit传递事件。然后只有e.preventDeafult()起作用

you could use trim() for remove unwanted spaces. 您可以使用trim()删除多余的空格。

Javascript Java脚本

function ValidCaptcha(e) {
  var str1 = document.getElementById('txtCaptcha').value.trim();
  var str2 = document.getElementById('txtCompare').value.trim();

  if (str1 == str2) {
    alert("請按OKAY兩次 (Please Press OKAY Twice To Submit)");
    document.getElementById('contact-form').action = "/send_form_email.php";
  } 
  else
  {
    alert("請輸入正確的號碼 (Please Enter The Correct Vertification Number)");
    e.preventDefault();
    e.stopPropagation();
  }
}

Html HTML

<form id="contact-form" class="form-horizontal" method="post" onsubmit="return ValidCaptcha(event);">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM