第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Recapcha V3 不適用于具有多種表單的頁面

Recapcha V3 不適用于具有多種表單的頁面

森林海 2021-06-17 18:48:13
我對 Google Recaptcha V3 有問題。Id 確實適用于單個表單,但如果頁面中的表單超過 1 個,則僅適用于第一個表單。如何使它適用于所有形式?我知道問題出在, id="recaptchaResponse"但我不知道如何解決這個問題!那里有一些類似的問題,但找不到解決方案。Javascript:<script src="https://www.google.com/recaptcha/api.js?render=key1"></script><script>    grecaptcha.ready(function () {        grecaptcha.execute('key2', { action: 'contact' }).then(function (token) {            var recaptchaResponse = document.getElementById('recaptchaResponse');            recaptchaResponse.value = token;        });    });</script>形式:<form class="user" action="" method="post" name="form1" id="form1">    <input type="hidden" name="source" value="form1">    <button type="submit" class="btn btn-success">Submit 1</button>    <input type="hidden" name="recaptcha_response" id="recaptchaResponse"></form><form class="user" action="" method="post" name="form2" id="form2">    <input type="hidden" name="source" value="form2">    <button type="submit" class="btn btn-success">Submit 2</button>    <input type="hidden" name="recaptcha_response" id="recaptchaResponse"></form>請幫忙!提前致謝!
查看完整描述

2 回答

?
Qyouu

TA貢獻1786條經(jīng)驗 獲得超11個贊

問題似乎是因為getElementById調(diào)用只解析recaptcha_response第一種形式的輸入元素,而不是第二種形式。


一個簡單的解決方法是將recaptcha_response每種形式中元素的 id 更改為不同的東西,比如recaptchaResponse1和recaptchaResponse2。那么用于設(shè)置令牌的 Javascript 代碼可能是:


grecaptcha.ready(function () {

  grecaptcha.execute('key2', { action: 'contact' }).then(function (token) {

    document.getElementById('recaptchaResponse1').value = token;

    document.getElementById('recaptchaResponse2').value = token;

  });

});

一種更易于維護且適用于任意數(shù)量表單的更好方法是為recaptcha_reponse輸入指定一個類名,并使用該querySelectorAll函數(shù)獲取具有給定類名的所有輸入并更新它們。


<form class="user" action="" method="post" name="form1" id="form1">

    <input type="hidden" name="source" value="form1">

    <button type="submit" class="btn btn-success">Submit 1</button>


    <input type="hidden" name="recaptcha_response" class="recaptchaResponse">

</form>


<form class="user" action="" method="post" name="form2" id="form2">

    <input type="hidden" name="source" value="form2">

    <button type="submit" class="btn btn-success">Submit 2</button>


    <input type="hidden" name="recaptcha_response" class="recaptchaResponse">

</form>

grecaptcha

  .execute("key", {

    action: "contact"

  })

  .then(function(token) {

    document

      .querySelectorAll(".recaptchaResponse")

      .forEach(elem => (elem.value = token))

    ;

  });

希望有幫助:)


查看完整回答
反對 回復(fù) 2021-06-24
?
哆啦的時光機

TA貢獻1779條經(jīng)驗 獲得超6個贊

刪除 id 標(biāo)簽并僅使用名稱標(biāo)簽。


grecaptcha.ready(function () {

    grecaptcha.execute({publicKey}, {action: 'forms'}).then(function (token) {

        var recaptchaElements = document.getElementsByName('recaptcha');

        for (var i = 0; i < recaptchaElements.length; i++) {

            recaptchaElements[i].value = token;

        }

    });

});


查看完整回答
反對 回復(fù) 2021-06-24
  • 2 回答
  • 0 關(guān)注
  • 150 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號