我正在尝试通过谷歌实现新的隐形 recaptcha。
但我需要输入,应该在执行 recaptcha 之前验证表单。
我在这样的 recaptcha 回调函数上遇到错误:
未捕获的类型错误:document.getElementById() 提交不是函数
那么在执行验证和重新验证后如何提交表单?
HTML:
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<form id="form" action="?" method="post">
Name: (required) <input id="field" name="field">
<div id='recaptcha' class="g-recaptcha"
data-sitekey="6LcAmBAUAAAAAFukLQIkOIICuBBxKEdn-Gu83mcH"
data-callback="onSubmit"
data-size="invisible"></div>
<button id='submit'>submit</button>
</form>
<script>onload();</script>
Javascript:
function onSubmit(token) {
alert('Thanks ' + document.getElementById('field').value + '!');
document.getElementById('form').submit(); // This is error line
}
function validate(event) {
event.preventDefault();
if (!document.getElementById('field').value) {
alert("Please enter your name.");
} else {
grecaptcha.execute();
}
}
function onload() {
var element = document.getElementById('submit');
element.onclick = validate;
}
JSFiddle:http: //jsfiddle.net/dp1cLh28/6/