Javascript ReCaptcha 2.0:如果 recaptcha 成功,则在回调时启用提交按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30018213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:30:07  来源:igfitidea点击:

ReCaptcha 2.0: enable Submit button on callback if recaptcha successful

javascriptphpjquerycaptcharecaptcha

提问by thanks_in_advance

I have a very simple form as follows. I want to make it so that the Submit button is disabled, and only enabled afterthe user has successfully completed the ReCaptcha.

我有一个非常简单的表格如下。我想让提交按钮被禁用,并且只有用户成功完成 ReCaptcha后才启用。

I'm assuming I'm going to need some Javascript / jQuery to do this.

我假设我需要一些 Javascript / jQuery 来做到这一点。

Google's documentation on ReCaptcha 2.0 seems really sparse and dense (to me, anyway). I'd appreciate some pointers:

谷歌关于 ReCaptcha 2.0 的文档似乎非常稀疏和密集(无论如何对我来说)。我很感激一些指示:

<form action="something.php" method="post">
    Name: <input type="text" size="40" name="name"><br><br>
    <div class="g-recaptcha" data-sitekey="############-#####"></div>
    <input type="submit" value="Submit" >
</form>

回答by panda.o24

i did the same thing on my test site. however, i used a button instead of submit, so here:

我在我的测试站点上做了同样的事情。然而,我使用了一个按钮而不是提交,所以在这里:

you must add the property data-callback="enableBtn"data-callback property executes the function specified after accomplishment of recaptcha.

您必须添加属性data-callback="enableBtn"data-callback 属性执行 recaptcha 完成后指定的功能。

<div class="g-recaptcha" data-sitekey="############-#####" data-callback="enableBtn"></div>

and set the id of the button to whatever id you want to and set it to disabled:

并将按钮的 id 设置为您想要的任何 id 并将其设置为禁用:

<input type="button" value="Submit" id="button1" disabled="disabled">

then on javascript make a function to enable the button

然后在 javascript 上创建一个函数来启用按钮

 function enableBtn(){
   document.getElementById("button1").disabled = false;
 }

hope it helps.

希望能帮助到你。