我在同一页面上有超过4次google recaptcha。我设法让他们显示,但验证不起作用。
$('#newsletter').on('submit', function(e) {
var response = grecaptcha.getResponse();
if (response.length == 0) {
e.preventDefault();
alert("Incorrect captcha code. Please try again.");
return false;
} else {
return true;
}
});

<form action="" method="post" id="newsletter">
<div id="sideRecaptcha" class="g-recaptcha"></div>
<input type="submit" value="submit" />
</form>
<script src="https://www.google.com/recaptcha/api.js?onload=reCaptchaCallback&render=explicit&hl=en" async defer></script>
<script>
var reCaptchaCallback = function() {
var elements = document.getElementsByClassName('g-recaptcha');
for (var i = 0; i < elements.length; i++) {
var id = elements[i].getAttribute('id');
var widgetId = grecaptcha.render(id, {
'sitekey' : 'XXXXXXXX',
theme' : 'light'
});
elements[i].setAttribute('cid', widgetId);
grecaptcha.reset(widgetId);
}
};
</script>
&#13;
grecaptcha.getResponse()始终返回空。有什么想法吗?