使用 Google 隐形验证码时,自动 html 表单验证和必需属性不起作用。为什么?
When using Google Invisible Captcha automatic html form validation and required attribute does not work. Why?
我正在尝试实施新的 Google Invisible Recaptcha,但由于某些我还不知道的原因,自动 html 表单验证和必需的属性似乎与重新验证。我猜这是因为 onSubmit() 函数回调。有人可以让我知道如何解决这个问题吗?提前致谢。
下面是我使用 Google Invisible Recaptcha 实现的表单。数据站点密钥已被有意删除。
<html>
<head>
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<script>
function onSubmit(token) {
document.getElementById("contactForm").submit();
}
</script>
</head>
<body>
<form id="contactForm" action="#" method="POST">
<p>Leave a Message</p>
<label for="inputName">Name</label>
<input type="text" name="Name" id="inputName" placeholder="Name" required> <br>
<label for="inputEmail">Email</label>
<input type="email" name="Email" id="inputEmail" placeholder="Email Id" required> <br>
<label for="inputMessage">Message</label>
<input type="text" name="Message" id="inputMessage" placeholder="Message" required><br/>
<button class="g-recaptcha" type="submit" data-sitekey="//site-key" data-badge="bottomleft" data-callback='onSubmit'>Submit</button>
<button type="reset">Reset</button>
</form>
</body>
</html>
创建一个回调函数,将提交按钮的onclick 属性设置为onSubmit函数,然后触发点击。
<script>
function callBack(){
document.getElementById('submit-button').addEventListener('click',onSubmit);
document.getElementById('submit-button').click();
}
function onSubmit() {
//validation code here
document.getElementById("contactForm").submit();
}
</script>
将此回调函数设置为数据回调属性
<button id='submit-button' class="g-recaptcha" type="submit" data-sitekey="//site-key" data-badge="bottomleft" data-callback='callBack'>Submit</button>
由于新的 v2 grecaptcha 方法,您必须以编程方式执行此操作:grecaptcha.execute() 这样 recaptcha 就不会替换按钮的默认单击事件,该事件阻止了默认的 HTML5 表单验证.
见 from 。
我正在尝试实施新的 Google Invisible Recaptcha,但由于某些我还不知道的原因,自动 html 表单验证和必需的属性似乎与重新验证。我猜这是因为 onSubmit() 函数回调。有人可以让我知道如何解决这个问题吗?提前致谢。
下面是我使用 Google Invisible Recaptcha 实现的表单。数据站点密钥已被有意删除。
<html>
<head>
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<script>
function onSubmit(token) {
document.getElementById("contactForm").submit();
}
</script>
</head>
<body>
<form id="contactForm" action="#" method="POST">
<p>Leave a Message</p>
<label for="inputName">Name</label>
<input type="text" name="Name" id="inputName" placeholder="Name" required> <br>
<label for="inputEmail">Email</label>
<input type="email" name="Email" id="inputEmail" placeholder="Email Id" required> <br>
<label for="inputMessage">Message</label>
<input type="text" name="Message" id="inputMessage" placeholder="Message" required><br/>
<button class="g-recaptcha" type="submit" data-sitekey="//site-key" data-badge="bottomleft" data-callback='onSubmit'>Submit</button>
<button type="reset">Reset</button>
</form>
</body>
</html>
创建一个回调函数,将提交按钮的onclick 属性设置为onSubmit函数,然后触发点击。
<script>
function callBack(){
document.getElementById('submit-button').addEventListener('click',onSubmit);
document.getElementById('submit-button').click();
}
function onSubmit() {
//validation code here
document.getElementById("contactForm").submit();
}
</script>
将此回调函数设置为数据回调属性
<button id='submit-button' class="g-recaptcha" type="submit" data-sitekey="//site-key" data-badge="bottomleft" data-callback='callBack'>Submit</button>
由于新的 v2 grecaptcha 方法,您必须以编程方式执行此操作:grecaptcha.execute() 这样 recaptcha 就不会替换按钮的默认单击事件,该事件阻止了默认的 HTML5 表单验证.
见