gpt4 book ai didi

javascript - (Javascript)如何删除警报框后的 'undefined'

转载 作者:行者123 更新时间:2023-12-01 03:35:06 24 4
gpt4 key购买 nike

/* 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();
}
}

这是我在我的网站上用于 javascript captcha 元素的代码,该代码运行良好,但是弹出警报框后,始终会出现“未定义”。我已经尝试过 return true 和 return false 但没有运气。非常感谢<3

顺便说一句,这就是我在 HTML 中启动 javascript 表单 onsubmit 的方式

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

最佳答案

当你调用提交时

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

您有一条警报。您的 ValidCaptcha 不会返回任何内容,因此它是未定义的。而且警报不返回任何内容,因此没有意义。

应该是

onsubmit="return ValidCaptcha();"

现在你应该返回一个 bool 值。

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;
}
}

关于javascript - (Javascript)如何删除警报框后的 'undefined',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44334112/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com