简体   繁体   中英

click button at end of function

I'm using a stripe checkout button to pop up when someone clicks "create account" and then once they put in their credit card info the popup closes.

how would i get the "create account" button to be clicked programmatically after the popup closes?

Here's the sequence 1) user clicks create account button 2) popup opens 3) popup closes and then button is clicked programmatically

I assume it's something like this but I haven't gotten it working yet. Button ID is #create_stripe

$("#create_stripe").click();

Code:

<script>
  var handler = StripeCheckout.configure({
    key: 'pk_test',
    image: '/img/documentation/checkout/marketplace.png',
    locale: 'auto',
    token: function(token) {
      // Use the token to create the charge with a server-side script.
      // You can access the token ID with `token.id`
    }
  });

  $('#create_stripe').on('click', function(e) {
    // Open Checkout with further options
    handler.open({
      name: 'Demo Site',
      description: '2 widgets',
      amount: 2000
    });
    e.preventDefault();
  });

  // Close Checkout on page navigation
  $(window).on('popstate', function() {
    handler.close();

  });


</script>

You should use $("#create_stripe").click() . See click() in jQuery . Use it directly however :`

$(window).on('popstate', function() {
   $("#create_stripe").click()
});

Either of these:

document.getElementById('create_stripe').click(); 
$('#create_stripe').click();

Just trigger it. Since you want to execute the code you've placed inside of a click event, you can trigger it with $("#create_stripe").trigger('click')

Edit: in light of your comments below, I think you need something like this:

  var handler = StripeCheckout.configure({
    key: 'pk_test',
    image: '/img/documentation/checkout/marketplace.png',
    locale: 'auto',
    token: function(token) {
      // Use the token to create the charge with a server-side script.
      // You can access the token ID with `token.id`
    },
    closed: function (){
        $("#create_stripe").trigger('click')
    }
  });

You should use trigger() method. click() method is ambiguous because it works as a listener and as a trigger. So if you call trigger you don't have ambiguous behaviours:

 $(element).trigger('click'); // or the event you need

More info:

http://api.jquery.com/trigger/

使用$( "#foo" ).trigger( "click" );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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