我正在使用谷歌验证码
<label for="template-contactform-botcheck">Botcheck</label>
<div id="recaptcha_sme"></div>
我想验证它是否被选中,是否已被填充以及我这样做的方式是通过这段代码(引用下面)
if ($("#recaptcha_sme #recaptcha-anchor").hasClass("recaptcha-checkbox-checked")) {
alert("the captcha has been filled");
} else {
alert("Please fill the captcha!");
}
我不打算通过服务器端验证(google recaptcha API 检查),我只是想在 google recaptcha 被检查或填充时收到通知(使用警报提示)。有什么想法、线索、建议、建议吗?
下面是我完整的表单提交脚本代码(jquery中的提交函数)。
$("#sme_application_dialog form").submit(function(e){
if ($(this).find("#recaptcha_sme #recaptcha-anchor").hasClass("recaptcha-checkbox-checked")) {
alert("the captcha has been filled");
} else {
alert("Please fill the captcha!");
}
e.preventDefault();
});
正如您从上面的引用中看到的那样,我只是试图检查 ID 为“recaptcha-anchor”的元素是否具有“recaptcha-checkbox-checked”类(正如我从 DOM 结构中看到的那样)使用 chrome dom 资源管理器,该类已添加到具有“recaptcha-anchor 每次检查或已填充 google recaptcha 时”id 的元素,所以我认为这可能有效,但遗憾的是它不起作用。
PS:假设我有一个 ID 为“sme_application_dialog_form”的 div,并且在其中有一个 from 和 inside that form,有 google recaptcha,我有“$(document).ready”。假设一切都已设置并正常工作(除了我正在检查 google recaptcha 的验证已被选中或已被填充)
最佳答案
将 data-callback
添加到您的 ReCapthca
div:
<div class="g-recaptcha" data-sitekey="***" data-callback="recaptchaCallback"></div>
将此函数添加到您的代码中。
var recaptchachecked;
function recaptchaCallback() {
//If we managed to get into this function it means that the user checked the checkbox.
recaptchachecked = true;
}
您现在可以在 OnSubmit() 函数上使用 recaptchachecked。
关于javascript - 检查google recaptcha是否被选中或已被填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32218183/