谷歌reCaptcha内甜蜜警报错误

时间:2018-04-25 06:24:49

标签: javascript jquery recaptcha 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触发器,但似乎无法获取该元素。

弹出框完全加载后有没有办法执行功能?或者我在这里做错了什么。

对此有任何帮助将不胜感激。感谢。

2 个答案:

答案 0 :(得分:3)

以下是SweetAlert2的解决方案 - SweetAlert支持的分支:

model = gurobi_read(model.mps);
model.A(model.sense=='>',:) = -model.A(model.sense=='>',:);
model.rhs(model.sense=='>',:) = -model.rhs(model.sense=='>',:);
model.sense(model.sense=='>') = '<';
result = gurobi(model);
time = result.runtime;
function showSweetAlertRecaptcha() {
  Swal.fire({
    title: 'SweetAlert2 + Recaptcha',
    html: '<div id="recaptcha"></div>',
    onOpen: 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;
}

答案 1 :(得分:0)

使用jquery中的[array([[ 3.6053008e-01, 2.3023001e+01], [ 3.7837738e-01, -5.3823519e-01], [-5.9431229e-02, 1.9144805e-01], [ 3.1319150e-01, 7.1097821e-01], [ 2.3572737e-01, -1.1478594e-01], [ 1.0371532e-01, 6.1731148e-01], [ 9.7601610e-01, 1.4972438e+00], [ 2.3793930e-01, -7.1887998e-03], [ 5.5228376e-01, 4.6662283e-01], [ 1.2852958e+00, 4.7578201e-01], [ 1.3585456e-01, 5.0958228e-01], [-1.3454953e-01, -7.4981648e-01], [ 2.8292700e-03, -5.3929615e-01], [-2.1484761e-01, 8.2391448e-02], [ 9.3787408e-01, 8.5099733e-01], [-4.6459877e-01, 2.1304554e-01]], dtype=float32), array([-0.01857078, 0.26882732], dtype=float32)] 而非使用$(element).ready来解决问题

由于重新网站密钥对此网站无效,

以下提供的代码段无法正常运行

&#13;
&#13;
.onload
&#13;
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);
&#13;
&#13;
&#13;