检查是否已选中google recaptcha或已将其填满

Jul*_*eto 5 javascript jquery recaptcha

我正在使用Google Recaptcha

<label for="template-contactform-botcheck">Botcheck</label>
<div id="recaptcha_sme"></div>
Run Code Online (Sandbox Code Playgroud)

我想验证其是否已选中,是否已填充以及即时通信的方式是通过此代码(请参阅下文)

if ($("#recaptcha_sme #recaptcha-anchor").hasClass("recaptcha-checkbox-checked")) {
    alert("the captcha has been filled");
} else {
    alert("Please fill the captcha!");
}
Run Code Online (Sandbox Code Playgroud)

而且我不打算通过服务器端验证(检查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();


});
Run Code Online (Sandbox Code Playgroud)

正如您从上述参考资料中看到的那样,我只是在尝试检查ID为“ recaptcha-anchor”的元素是否具有“ recaptcha-checkbox-checked”类(如我从使用chrome dom的DOM结构中看到的那样)资源管理器中,该类已将其添加到ID为“每次检查或重新填充Google Recaptcha时都具有recaptcha-anchor”的元素,因此我认为这可能有效,但不幸的是,它不起作用。

PS:假设我有一个id为“ sme_application_dialog_form”的div,并且在该表单中有一个from和inside形式,有google recaptcha,我有“ $(document).ready”。假设一切都设置并正常工作(除了我正在检查google recaptcha的验证是否已检查或已填充)

Vig*_*war 1

您可以通过获取响应值是否为空来在回调函数上验证它。

回调函数

function submit(){ 
     var response = grecaptcha`.`getResponse();
     if(response != '0'){
           //captcha validated and got response code
           alert("the captcha has been filled");
     }else{
           //not validated or not clicked
           alert("Please fill the captcha!");
     }
}
Run Code Online (Sandbox Code Playgroud)

它会起作用的。