我有一个使用AJAX的注册表单,以便我想在任何时候发生错误(即用户名已经在使用)刷新Recaptcha图像。
我正在寻找与ReCaptcha兼容的代码,使用JavaScript重新加载它。
我有一个使用AJAX的注册表单,以便我想在任何时候发生错误(即用户名已经在使用)刷新Recaptcha图像。
我正在寻找与ReCaptcha兼容的代码,使用JavaScript重新加载它。
试试这个
<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>
如果调用showRecaptcha,验证码将被一个新的验证码实例填充。
对于reCaptcha v2,使用:
grecaptcha.reset();
如果你正在使用reCaptcha v1(可能没有):
Recaptcha.reload();
如果窗口上已经加载了重新验证码,这就可以了。
(根据下面@SebiH的评论更新。)
或者,您可以模拟单击刷新按钮
// If recaptcha object exists, refresh it
if (typeof Recaptcha != "undefined") {
jQuery('#recaptcha_reload').click();
}
如果您正在使用新的recaptcha 2.0,请使用以下命令: 后面的代码:
ScriptManager.RegisterStartupScript(this, this.GetType(), "CaptchaReload", "$.getScript(\"https://www.google.com/recaptcha/api.js\", function () {});", true);
对于简单的javascript
<script>$.getScript(\"https://www.google.com/recaptcha/api.js\", function () {});</script>
重要提示:不再支持reCAPTCHA API的1.0版本。请升级到2.0版本。
你可以使用grecapcha .reset();重置验证码。
来源:https://developers.google.com/recaptcha/docs/display js_api
grecaptcha.reset(opt_widget_id)
重置reCAPTCHA小部件。可以传递一个可选的小部件id,否则函数将重置创建的第一个小部件。(来自谷歌的网页)