简体   繁体   English

功能结束时单击按钮

[英]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 顺序如下:1)用户单击创建帐户按钮2)弹出窗口打开3)弹出窗口关闭,然后以编程方式单击按钮

I assume it's something like this but I haven't gotten it working yet. 我以为是这样,但我还没有开始工作。 Button ID is #create_stripe 按钮ID为#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() . 您应该使用$("#create_stripe").click() See click() in jQuery . 参见jQuery中的click() 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') 由于要执行放置在click事件中的代码,因此可以使用$("#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. 您应该使用trigger()方法。 click() method is ambiguous because it works as a listener and as a trigger. click()方法是模棱两可的,因为它既充当侦听器又充当触发器。 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/ http://api.jquery.com/trigger/

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

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

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