简体   繁体   English

如何在复选框激活时禁用/启用 PayPal 按钮?

[英]How to disable/enable PayPal button on Checkbox activation?

I have a page on my site that is setup for recurring payments, but I want to disable the PayPal checkout buttons until someone has checked the box that agrees to my Terms Of Service.我的网站上有一个页面用于定期付款,但我想禁用 PayPal 结帐按钮,直到有人选中同意我的服务条款的框。 Can you help me figure out how to do this properly?你能帮我弄清楚如何正确地做到这一点吗?

THANKS!!!!!谢谢!!!!! -Brad -布拉德

<p id="error" class="hidden">Please check the checkbox</p>
<label><input id="check" type="checkbox"> Check here to continue</label>

<script>
  paypal.Buttons({

    // onInit is called when the button first renders
    onInit: function(data, actions) {

      // Disable the buttons
      actions.disable();

      // Listen for changes to the checkbox
      document.querySelector('#check')
        .addEventListener('change', function(event) {

          // Enable or disable the button when it is checked or unchecked
          if (event.target.checked) {
            actions.enable();
          } else {
            actions.disable();
          }
        });
    },

    // onClick is called when the button is clicked
    onClick: function() {

      // Show a validation error if the checkbox is not checked
      if (!document.querySelector('#check').checked) {
        document.querySelector('#error').classList.remove('hidden');
      }
    }

  }).render('#paypal-button-container');
</script>

I know this is an old thread but thought Id post the what I did.我知道这是一个旧线程,但我想我会发布我所做的。 Simply, all you need to do is give the button an id and set to disabled, use java to enable the button on checkbox checked, apply the code to the checkbox onclick="UnlockButton()".简单地说,您需要做的就是给按钮一个 id 并设置为禁用,使用 java 启用复选框选中的按钮,将代码应用于复选框 onclick="UnlockButton()"。 That worked for me.那对我有用。

function UnlockButton(){
  if(document.getElementById("checkboxID").checked == true)
  {
    document.getElementById("buttonid").disabled = false;
  }
  else{
    document.getElementById("buttonid").disabled  = true;
  }
  return true;
}

 // Listen to whether the checkbox is clicked. document.getElementById('terms').addEventListener('click', function() { if (this.checked) { document.getElementById('paypal-disabled').style.display = 'none'; } else { document.getElementById('paypal-disabled').style.display = 'block'; } }); // Disable the ability to access the paypal button using the tab key. document.getElementById('disable-tabing').addEventListener('focus', function() { document.getElementById('conditions-link').focus(); });
 #paypal-container { background-color: #ffc439; height: 2em; width: 5em; cursor: pointer; margin-top: 1em; } #paypal { margin: auto; position: relative; top: 0.4em; left: 1em; } #paypal:hover { color: white; } #paypal-disabled { background-color: white; height: 3em; opacity: 0.5; position: relative; bottom: 2.5em; } #disabled-content { opacity: 0; } #disable-tabing { opacity: 0; }
 <!-- Terms & Conditions --> <input type="checkbox" name="terms" value="accept" id="terms"><small>By checking this box you agree to the <a href="#" id="conditions-link">Terms and Conditions</a>.</small> <!-- The text input below is used to keep the user from being able to access the paypal button using the 'tab' key. Every time the text field is focused, it triggers an event which focuses on the element that precedes it. Set the opacity to 0. --> <input type="text" id="disable-tabing"> <!-- This is the div the paypal button will be rendered in. --> <div id="paypal-container"> <!-- Ignore the <span>, it's just for presentation. --> <span id="paypal">Paypal</span> </div> <!-- This div is used to cover-up the paypal button. Set it's position to relative and move it on top of the paypal button. Set it's opacity to make it translucent. --> <div id="paypal-disabled"> <!-- Ignore the <span>, it's just for presentation. --> <span id="disabled-content">Disabled</span> </div>

I know this answer is late, but I thought I would still post it just in case someone can use it.我知道这个答案来晚了,但我想我还是会发布它以防万一有人可以使用它。 You cannot " disable " the Paypal button because it's being rendered by Paypal .您不能“禁用” Paypal 按钮,因为它是由Paypal呈现的。 Although I'm open to being proved wrong about that.尽管我愿意在这方面被证明是错误的。 Instead I've figured out a method which basiclly does the same thing—it keeps the user from being able to click the contents of the div, and from accessing it via the 'tab' key.相反,我想出了一种基本上做同样事情的方法——它使用户无法点击 div 的内容,也无法通过“tab”键访问它。
Here's the JSfiddle : Disable Paypal Button这是JSfiddle禁用 Paypal 按钮

I'm not gonna give you a full answer written in code because I don't know the elements or variable names in your code.我不会给你用代码写的完整答案,因为我不知道你代码中的元素或变量名称。 Also it's better to write your own and learn.此外,最好自己编写并学习。

Use jquery to add a .toggle() function on the checkbox item.使用 jquery 在复选框项上添加 .toggle() 函数。 You can select the checkbox item via its id (or class if you wish).您可以通过其 id(或类,如果您愿意)来选择复选框项目。

You can create a div called 'paypal' and place the paypal button inside it.您可以创建一个名为“paypal”的 div 并将 paypal 按钮放在其中。 Then,然后,

Something like:就像是:

$('#checkbox').toggle(function() {
    $('#paypal').show();
}, function() {
    $('#paypal').hide();
});

Hope this helps.希望这可以帮助。

It wont work, PAYPAL button is a tag, so Disabling it is not like disabling any DIV or BUTTON;它不起作用,PAYPAL 按钮是一个标签,所以禁用它不像禁用任何 DIV 或 BUTTON; Im trying to figure it out to but not found yet.我想弄清楚但还没有找到。

The solution is to just put your validation in another function and then call it from both places.解决方案是将您的验证放在另一个函数中,然后从两个地方调用它。

So something like:所以像:

function validateFields() {

      if (document.querySelector('#FirstName').value.length <= 1) {
        document.querySelector('#error').innerText = "Please enter a first name";
        return false;
      }

      if (document.querySelector('#LastName').value.length <= 1) {
        document.querySelector('#error').innerText = "Please enter a last name";
        return false;
      }
      if (document.querySelector('#Phone').value.length <= 1) {
        document.querySelector('#error').innerText = "Please enter a phone number";
        return false;
      } 

      if (document.querySelector('#Email').value.length <= 1) {
        document.querySelector('#error').innerText = "Please enter an email";
        return false;
      } 

      document.querySelector('#error').innerText = "";
      return true;

}

Then if it's valid you can enable the actions in the init just like in the paypal sample:然后,如果它有效,您可以像在 paypal 示例中一样启用 init 中的操作:

  onInit: function(data, actions) {

    // Disable the buttons
    actions.disable();

    // Listen for changes to the input
    document.querySelector('#userForm input')
      .addEventListener('blur', function(event) {
        if (validateFields()) {
          actions.enable();
        } 
      });
  },

And then also run your validation on the onclick然后也在 onclick 上运行您的验证

  onClick: function(data, actions) {
    if (validateFields()) {
      return true;
    }
    document.querySelector('#error').classList.remove('hidden');
  }    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM