问题
This is my code
$(document).ready(function(){
$(":submit").click(function(){
var onloadCallback = function() {
$( "#submitlogin" ).each(function() {
grecaptcha.render($( this ).attr('id'), {
'sitekey' : '6Lcjvi4UAAAAAIR7_ckItF2HfMEFHx427WUDdGDk',
'callback' : onsubmitlogin
});
});
$( "#submitsignup" ).each(function() {
grecaptcha.render($( this ).attr('id'), {
'sitekey' : '6Lcjvi4UAAAAAIR7_ckItF2HfMEFHx427WUDdGDk',
'callback' : onsubmitsignup
});
});
$( "#submitforgotpass" ).each(function() {
grecaptcha.render($( this ).attr('id'), {
'sitekey' : '6Lcjvi4UAAAAAIR7_ckItF2HfMEFHx427WUDdGDk',
'callback' : onsubmitforgotpass
});
});
};
});
});
I want to load onloadcallback
function, only when submit
button is clicked.
But seems like the code inside it, never gets called. What's the problem?
If the problem is not in this code, then let me know, I will update my answer with more code from my original website
回答1:
You defined onloadcallback
but never called it when button is clicked. This will work:
// Define callback function
var onloadCallback = function() {
$("#submitlogin").each(function() {
grecaptcha.render($(this).attr('id'), {
'sitekey': '6Lcjvi4UAAAAAIR7_ckItF2HfMEFHx427WUDdGDk',
'callback': onsubmitlogin
});
});
$("#submitsignup").each(function() {
grecaptcha.render($(this).attr('id'), {
'sitekey': '6Lcjvi4UAAAAAIR7_ckItF2HfMEFHx427WUDdGDk',
'callback': onsubmitsignup
});
});
$("#submitforgotpass").each(function() {
grecaptcha.render($(this).attr('id'), {
'sitekey': '6Lcjvi4UAAAAAIR7_ckItF2HfMEFHx427WUDdGDk',
'callback': onsubmitforgotpass
});
});
};
// Use onloadCallback as event handler
$(":submit").click(onloadCallback);
来源:https://stackoverflow.com/questions/45996424/how-to-call-google-recaptcha-code-on-submit-button-click