最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Reset Multiple Google reCAPTCHA - Stack Overflow

programmeradmin0浏览0评论

I have two forms on a single page which both use Google reCAPTCHA.

Whenever i use grecaptcha.reset(); it only resets the first instance.

The following JS renders the reCAPTCHA(s)

var recaptcha1;
var recaptcha2;

var myCallBack = function() {
    //Render the recaptcha1 on the element with ID "recaptcha1"
    recaptcha1 = grecaptcha.render('recaptcha1', {
        'sitekey' : '1234', //Replace this with your Site key
        'theme' : 'light'
    });

    //Render the recaptcha2 on the element with ID "recaptcha2"
    recaptcha2 = grecaptcha.render('recaptcha2', {
        'sitekey' : '1234', //Replace this with your Site key
        'theme' : 'light'
    });
};

How can i reset all instances or target a specific instance.

I have two forms on a single page which both use Google reCAPTCHA.

Whenever i use grecaptcha.reset(); it only resets the first instance.

The following JS renders the reCAPTCHA(s)

var recaptcha1;
var recaptcha2;

var myCallBack = function() {
    //Render the recaptcha1 on the element with ID "recaptcha1"
    recaptcha1 = grecaptcha.render('recaptcha1', {
        'sitekey' : '1234', //Replace this with your Site key
        'theme' : 'light'
    });

    //Render the recaptcha2 on the element with ID "recaptcha2"
    recaptcha2 = grecaptcha.render('recaptcha2', {
        'sitekey' : '1234', //Replace this with your Site key
        'theme' : 'light'
    });
};

How can i reset all instances or target a specific instance.

Share Improve this question asked Jul 10, 2015 at 15:27 Ryan ErringtonRyan Errington 831 silver badge5 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 8

The answer by mbejda is almost correct but not quite.

Through trial and error, I found that you have to do this:

grecaptcha.reset(recaptcha1);
grecaptcha.reset(recaptcha2);

To be clear, you have to save the return values of grecaptcha.render(), and pass in those vars to the reset() function. Passing in the string ID of the captcha doesn't work.

grecaptcha.reset(0);
grecaptcha.reset(1);

A more dynamic approach to iterate over the captcha controls regardless of how many they are and reset them all. (using jQuery):

    var c = $('.g-recaptcha').length;
    for (var i = 0; i < c; i++)
        grecaptcha.reset(i);

According to googles documentation here. https://developers.google.com/recaptcha/docs/display#js_param
You should be able to pass the captcha Id into the reset method.
grecaptcha.reset(widgetId);

So then you can do :


    grecaptcha.reset("recaptcha1");
    grecaptcha.reset("recaptcha2");

发布评论

评论列表(0)

  1. 暂无评论