如何使用 JavaScript 重新加载 ReCaptcha?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3371314/
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
How to Reload ReCaptcha using JavaScript?
提问by AhmetB - Google
I have a signup form with AJAX so that I want to refresh Recaptcha image anytime an error is occured (i.e. username already in use).
我有一个带有 AJAX 的注册表单,因此我想在发生错误时刷新 Recaptcha 图像(即用户名已在使用中)。
I am looking for a code compatible with ReCaptcha to reload it using JavaScript.
我正在寻找与 ReCaptcha 兼容的代码以使用 JavaScript 重新加载它。
采纳答案by abhiagNitk
Important: Version 1.0 of the reCAPTCHA API is no longer supported, please upgrade to Version 2.0.
重要提示:不再支持 reCAPTCHA API 1.0 版,请升级到 2.0 版。
You can use grecaptcha.reset();to reset the captcha.
您可以使用grecaptcha.reset(); 重置验证码。
Source : https://developers.google.com/recaptcha/docs/verify#api-request
来源:https: //developers.google.com/recaptcha/docs/verify#api-request
回答by AhmetB - Google
For reCaptcha v2, use:
对于 reCaptcha v2,请使用:
grecaptcha.reset();
If you're using reCaptcha v1 (probably not):
如果您使用的是 reCaptcha v1(可能不是):
Recaptcha.reload();
This will do if there is an already loaded Recaptcha on the window.
如果window.
(Updated based on @SebiH's comment below.)
(根据@SebiH 在下面的评论更新。)
回答by Dasun
If you are using version 1
如果您使用的是版本 1
Recaptcha.reload();
If you are using version 2
如果您使用的是版本 2
grecaptcha.reset();
回答by Kutalia
grecaptcha.reset(opt_widget_id)
Resets the reCAPTCHA widget. An optional widget id can be passed, otherwise the function resets the first widget created. (from Google's web page)
重置 reCAPTCHA 小部件。可以传递可选的小部件 ID,否则该函数会重置创建的第一个小部件。(来自谷歌的网页)
回答by Tim
Try this
尝试这个
<script type="text/javascript" src="//www.google.com/recaptcha/api/js/recaptcha_ajax.js"></script>
<script type="text/javascript">
function showRecaptcha() {
Recaptcha.create("YOURPUBLICKEY", 'captchadiv', {
theme: 'red',
callback: Recaptcha.focus_response_field
});
}
</script>
<div id="captchadiv"></div>
If you calll showRecaptcha the captchadiv will be populated with a new recaptcha instance.
如果您调用 showRecaptcha,验证码将填充新的验证码实例。
回答by AlxVallejo
Or you could just simulate a click on the refresh button
或者你可以模拟点击刷新按钮
// If recaptcha object exists, refresh it
if (typeof Recaptcha != "undefined") {
jQuery('#recaptcha_reload').click();
}
回答by Marcelo C.
For AngularJS users:
对于 AngularJS 用户:
$window.grecaptcha.reset();
回答by Corporate purpose
For new version of reCaptcha v2use :
对于新版本的reCaptcha v2使用:
grecaptcha.reset();
回答by s sharif
If you are using Version 1.0 of the reCAPTCHA API, please upgrade to Version 2.0(Version 1.0 is no longer supported)
如果您使用的是 1.0 版的 reCAPTCHA API,请升级到 2.0版(不再支持 1.0 版)
Use grecaptcha.reset();to reset the captcha.
使用grecaptcha.reset();重置验证码。
回答by Sara Khan
if you are using new recaptcha 2.0 use this: for code behind:
如果您使用的是新的 recaptcha 2.0,请使用以下代码:
ScriptManager.RegisterStartupScript(this, this.GetType(), "CaptchaReload", "$.getScript(\"https://www.google.com/recaptcha/api.js\", function () {});", true);
for simple javascript
对于简单的 javascript
<script>$.getScript(\"https://www.google.com/recaptcha/api.js\", function () {});</script>

