Reset Multiple Google reCAPTCHA

家住魔仙堡 提交于 2021-02-18 00:49:32

问题


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.


回答1:


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.




回答2:


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



回答3:


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);



回答4:


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");



来源:https://stackoverflow.com/questions/31344626/reset-multiple-google-recaptcha

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!