JS-如何检查 Braintree 插件 ui 是否正确加载?

JS-How to check if Braintree drop-in ui loaded correctly?

如果 braintree 插件 ui 没有正确加载,表单提交按钮仍然可以点击。我想了解是否正确加载 ui 然后 enable/disable 按钮。

如何检测 braintree 插件 ui 是否正确加载?

如果您没有在插入 Braintree ui 的位置包含任何其他子元素,您可以使用下面的脚本检查是否存在子元素(braintree 设置应该添加)并且还将检查 braintree 对象是否在 window 上定义。注:childElementCount要求uires a polyfill支持IE8。

html:

<div id="braintreeCompHere"></div>

脚本:

checkForBraintree('braintreeCompHere');

function checkForBraintree(btContainer) {
    var elBool = document.getElementById(btContainer).childElementCount > 0,
        btBool = typeof window.braintree !== 'undefined';

    return elBool && btBool;
}

我找到了一个更简洁的解决方案:

  1. 禁用提交按钮属性
  2. 在 braintree.setup() 中使用 onReady 回调启用提交按钮。