Google SweetAlert 模态中的 reCaptcha window
Google reCaptcha within SweetAlert modal window
我正在开发一个需要在 SweetAlert 弹出框中进行 reCaptcha 验证的功能,但出现以下错误:
Uncaught Error: reCAPTCHA placeholder element must be an element or id
我一直在调查导致此错误的原因,我注意到这是因为当它尝试生成 reCaptcha 元素时该元素尚未加载。
function confirmBox() {
var div = document.createElement('div');
div.id = 'recaptcha';
div.onload = genRecaptcha();
swal({
title: "Are you sure?",
content: div,
icon: "warning",
buttons: true,
dangerMode: true,
})
}
function genRecaptcha() {
console.log($('#recaptcha').attr('id'));
grecaptcha.render('recaptcha', {
'sitekey': '6Le2ZFUUAAAAAEV4IM_5weEnXg4gVplan2Atgiaj'
});
}
$('#btn1').click(confirmBox);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<script src="https://www.google.com/recaptcha/api.js"></script>
<button id="btn1">Submit</button>
我正在尝试将 genRecaptcha
函数与 onload
触发器一起使用,但似乎无法获取元素。
有什么方法可以在弹框加载完成后执行该功能吗?还是我哪里做错了?
使用 jquery 中的 $(element).ready
解决问题,而不是使用 .onload
下面提供的代码片段将无法正常运行,因为 recaptcha 站点密钥对该站点无效
function confirmBox() {
var div = document.createElement('div');
div.id = 'recaptcha';
$('#recaptcha').ready(function () {
console.log($('#recaptcha').attr('id'));
grecaptcha.render('recaptcha', {
'sitekey': '6Le2ZFUUAAAAAEV4IM_5weEnXg4gVplan2Atgiaj'
});
})
swal({
title: "Are you sure?",
content: div,
icon: "warning",
buttons: true,
dangerMode: true,
})
}
$('#btn1').click(confirmBox);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<script src="https://www.google.com/recaptcha/api.js"></script>
<button id="btn1">Submit</button>
UPD:这是现场演示:https://sweetalert2.github.io/recipe-gallery/recaptcha.html
这是 SweetAlert2 的解决方案 - SweetAlert 的支持分支:
function showSweetAlertRecaptcha() {
Swal.fire({
title: 'SweetAlert2 + Recaptcha',
html: '<div id="recaptcha"></div>',
willOpen: function() {
grecaptcha.render('recaptcha', {
'sitekey': '6LdvplUUAAAAAK_Y5M_wR7s-UWuiSEdVrv8K-tCq'
});
},
preConfirm: function () {
if (grecaptcha.getResponse().length === 0) {
Swal.showValidationMessage(`Please verify that you're not a robot`)
}
}
})
}
#recaptcha > div {
width: auto !important;
margin-bottom: .5em;
}
<script src="https://www.google.com/recaptcha/api.js?onload=showSweetAlertRecaptcha"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
我正在开发一个需要在 SweetAlert 弹出框中进行 reCaptcha 验证的功能,但出现以下错误:
Uncaught Error: reCAPTCHA placeholder element must be an element or id
我一直在调查导致此错误的原因,我注意到这是因为当它尝试生成 reCaptcha 元素时该元素尚未加载。
function confirmBox() {
var div = document.createElement('div');
div.id = 'recaptcha';
div.onload = genRecaptcha();
swal({
title: "Are you sure?",
content: div,
icon: "warning",
buttons: true,
dangerMode: true,
})
}
function genRecaptcha() {
console.log($('#recaptcha').attr('id'));
grecaptcha.render('recaptcha', {
'sitekey': '6Le2ZFUUAAAAAEV4IM_5weEnXg4gVplan2Atgiaj'
});
}
$('#btn1').click(confirmBox);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<script src="https://www.google.com/recaptcha/api.js"></script>
<button id="btn1">Submit</button>
我正在尝试将 genRecaptcha
函数与 onload
触发器一起使用,但似乎无法获取元素。
有什么方法可以在弹框加载完成后执行该功能吗?还是我哪里做错了?
使用 jquery 中的 $(element).ready
解决问题,而不是使用 .onload
下面提供的代码片段将无法正常运行,因为 recaptcha 站点密钥对该站点无效
function confirmBox() {
var div = document.createElement('div');
div.id = 'recaptcha';
$('#recaptcha').ready(function () {
console.log($('#recaptcha').attr('id'));
grecaptcha.render('recaptcha', {
'sitekey': '6Le2ZFUUAAAAAEV4IM_5weEnXg4gVplan2Atgiaj'
});
})
swal({
title: "Are you sure?",
content: div,
icon: "warning",
buttons: true,
dangerMode: true,
})
}
$('#btn1').click(confirmBox);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<script src="https://www.google.com/recaptcha/api.js"></script>
<button id="btn1">Submit</button>
UPD:这是现场演示:https://sweetalert2.github.io/recipe-gallery/recaptcha.html
这是 SweetAlert2 的解决方案 - SweetAlert 的支持分支:
function showSweetAlertRecaptcha() {
Swal.fire({
title: 'SweetAlert2 + Recaptcha',
html: '<div id="recaptcha"></div>',
willOpen: function() {
grecaptcha.render('recaptcha', {
'sitekey': '6LdvplUUAAAAAK_Y5M_wR7s-UWuiSEdVrv8K-tCq'
});
},
preConfirm: function () {
if (grecaptcha.getResponse().length === 0) {
Swal.showValidationMessage(`Please verify that you're not a robot`)
}
}
})
}
#recaptcha > div {
width: auto !important;
margin-bottom: .5em;
}
<script src="https://www.google.com/recaptcha/api.js?onload=showSweetAlertRecaptcha"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>