我有一个表单,我使用隐形Recaptcha - 我想在提交之前验证字段。事实证明这是一个比预期更大的痛苦,主要是因为ReCaptcha实例将被渲染。但是 - 使用他们的文档 - 我已经建立了:
<html>
<head>
<script>
function onSubmit(token) {
alert("Submitting");
document.getElementById('myform').submit();
}
function validate(event) {
event.preventDefault();
if (!document.getElementById('field').value) {
alert("You must add text to the required field");
} else {
grecaptcha.execute();
}
}
function onload() {
var element = document.getElementById('submit');
element.onclick = validate;
}
</script>
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
</head>
<body>
<form id="myform" action="hello.php" method="POST">
Name: (required) <input id="field" name="field">
<div id='recaptcha' class="g-recaptcha"
data-sitekey="haoeghioaehgoiaegoiaeg2831850135"
data-callback="onSubmit"
data-size="invisible"></div>
<button id='submit'>submit</button>
</form>
<script>onload();</script>
</body>
</html>
现在,它将验证该字段已填写,它会提醒我,但它不会提交&#34;那个行动?它在那时停止。
我错过了什么?