我正在使用jquery validate插件。我在单页中使用了2个google recaptcha。所以代码与我们通常看到的有点不同。我正在验证响应是否为空或已检查。但我每次都会得到空白的反应(即使我检查了验证码)。
Html -
<div class="g-recaptcha" id="recaptcha1"></div>
<div class="g-recaptcha" id="recaptcha2"></div>
JS -
<script src="https://www.google.com/recaptcha/api.js?onload=myCallBack&render=explicit" async defer></script>
<script>
var recaptcha1;
var recaptcha2;
var myCallBack = function() {
//Render the recaptcha1 on the element with ID "recaptcha1"
recaptcha1 = grecaptcha.render('recaptcha1', {
'sitekey' : '6Ld28cISAAAAACLXX5xgaV1nemEoTw5uxnGhsyVc', //Replace this with your Site key
'theme' : 'dark'
});
//Render the recaptcha2 on the element with ID "recaptcha2"
recaptcha2 = grecaptcha.render('recaptcha2', {
'sitekey' : '6Ld28cISAAAAACLXX5xgaV1nemEoTw5uxnGhsyVc', //Replace this with your Site key
'theme' : 'dark'
});
};
</script>
Jquery验证插件
$(document).ready(function() {
$("form[name='contactform']").validate({
rules: {
yname: {
required: true,
lettersonly: true
},
...................
rest of the code
......................
submitHandler: function (form) {
//form.submit();
alert(grecaptcha.getResponse());
if (grecaptcha.getResponse()) {
form.submit();
} else {
alert('Please confirm captcha to proceed')
}
}
}
});
我是否需要检查文档外的响应?任何帮助都非常感谢。