簡體   English   中英

功能結束時單擊按鈕

[英]click button at end of function

當某人單擊“創建帳戶”時,我正在使用條紋簽出按鈕彈出,然后一旦他們輸入了信用卡信息,該彈出窗口就會關閉。

彈出窗口關閉后,如何通過編程方式單擊“創建帳戶”按鈕?

順序如下:1)用戶單擊創建帳戶按鈕2)彈出窗口打開3)彈出窗口關閉,然后以編程方式單擊按鈕

我以為是這樣,但我還沒有開始工作。 按鈕ID為#create_stripe

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

碼:

<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>

您應該使用$("#create_stripe").click() 參見jQuery中的click() 但是直接使用它:

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

這些:

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

只需觸發它即可。 由於要執行放置在click事件中的代碼,因此可以使用$("#create_stripe").trigger('click')觸發它

編輯:根據下面的評論,我認為您需要這樣的內容:

  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')
    }
  });

您應該使用trigger()方法。 click()方法是模棱兩可的,因為它既充當偵聽器又充當觸發器。 因此,如果您調用觸發器,則不會有模棱兩可的行為:

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

更多信息:

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM