ReCaptcha:如何在发送验证码时自动提交表单

问题描述:

我想使用ReCaptcha在页面上加载一些额外的数据。我希望在输入ReCaptcha时自动提交表单。所以我不需要额外的提交按钮。问题是recaptcha在iframe中加载了它的内容,所以有点困难。

I want to use ReCaptcha to load some extra data on the page. I want the form to be auto submitted when the ReCaptcha was entered. So that I don't need the extra submit button. The problem is that recaptcha loads its content inside an iframe, so its a bit difficult.

目前我有这样的表格:

<form action="javascript:getInfo(grecaptcha.getResponse(widget1));" >
      <div id="captcha"></div>
      <br>
      <input type="submit" value="Submit">
</form>

如何在提交外部表单的recaptcha提交中获得类似事件监听器的内容?

How do I get something like an Event-Listener on the recaptcha submit which submits the outer form?

这听起来像是一种有趣的技巧。它会减少用户的点击和击键。这是你如何做到这一点,通过听取成功的验证码响应,你将能够跟进所需的行动。这是一个示例和一些文档。 https://developers.google.com/recaptcha/docs/display#example

That's sounds like an interesting technique. It would cut down on the clicking and key strokes for the user. Here is how you could do that, by listening for the successful captcha response you would be able to follow up with the desired action. Here is an example and some documentation. https://developers.google.com/recaptcha/docs/display#example

var RC2KEY = 'sitekey';

function reCaptchaVerify(response) {
  if (response === document.querySelector('.g-recaptcha-response').value) {
    document.forms['form-name'].submit();
  }
}

function reCaptchaExpired() {
  /* do something when it expires */
}

function reCaptchaCallback() {
  grecaptcha.render('id', {
    'sitekey': RC2KEY,
    'callback': reCaptchaVerify,
    'expired-callback': reCaptchaExpired
  });
}

<script src='https://www.google.com/recaptcha/api.js?onload=reCaptchaCallback&render=explicit'></script>