代码之家  ›  专栏  ›  技术社区  ›  Dean

SweetAlert模式窗口中的Google reCaptcha

  •  2
  • Dean  · 技术社区  · 7 年前

    我正在开发一个函数,该函数需要在SweetAlert弹出框中执行reCaptcha验证,但我遇到以下错误:

    未捕获错误:reCAPTCHA占位符元素必须是元素或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 回复  |  直到 5 年前
        1
  •  5
  •   Limon Monte    3 年前

    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>
        2
  •  0
  •   Dean    7 年前

    使用解决问题 $(element).ready 从…起 jquery 而不是使用 .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>