问题描述
更新队列: 我正在尝试在表单验证后验证不可见的Google Recaptcha。如果有任何可疑活动,我知道此问题已由Google Recaptcha API修复。它一直在提供令牌(我正在所有侧边的卷曲调用上进行验证)
问题:提交表单后,将显示Recaptcha窗口,并且在Recaptcha验证后,页面不会自动提交。
我的脚本:
<script src="https://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit"
async defer></script>
<script type="text/javascript">
var onloadCallback = function() {
grecaptcha.render('g-recaptcha',{
'sitekey' : '6Lf7X68ZAAAAAMAyjWey_BRNhGvwi4jHBLIdzYjM','callback' : setResponse
});
};
function setResponse(response) {
alert(response);
document.getElementById('captcha-response').value = response;
}
jQuery(myForm).on('submit',function (e) {
e.preventDefault(); // avoid to submit
if (validation() == true) {
grecaptcha.execute();
}else {
this.submit();
}
});
</script>
<div id="g-recaptcha" class="g-recaptcha" data-size="invisible"></div>
<input type="hidden" id="captcha-response" name="captcha-response" />
解决方法
您的问题在以下一行:
jQuery(assessment_form).submit(function ()
为了避免提交表单,您可以将上一行更改为:
jQuery('#assessment_form').on('submit',function (e) {
e.preventDefault(); // avoid to submit
if (validation() == true) {
grecaptcha.execute();
} else {
this.submit();
}
});
另一种方法是将输入从type = submit更改为input type = button。