简体   繁体   English

jQuery 多次自动单击按钮

[英]Click button automatically more than once jQuery

When I click on a button I want to auto click it for example 4 times again after the click.当我点击一个按钮时,我想在点击后再次自动点击它4次。 So basically you fire it 5 times.所以基本上你发射它5次。 I have a code like this to retrieve the number it should be clicked.我有一个这样的代码来检索它应该被点击的号码。

$(document).ready(function() {

    var clickedTimes; 

    $( "form" ).change(function() {
     clickedTimes = jQuery('input#cfwc-title-field').val();
    });

    $('#single_add_to_cart_button').on('click', function(event){
        if ((clickedTimes-1)==0) {
                return;
        }
        clickedTimes -=1;
        console.log(`Click-Event triggered ${(5-(clickedTimes))} times.`);
        $( "#single_add_to_cart_button" ).trigger( "click" );
    });
});

For example the clickedTimes = 5例如clickedTimes = 5

After clicking the add to cart button beneath it it should auto fire 4 times again on it because I fired it once myself.单击其下方的添加到购物车按钮后,它应该会再次自动触发4次,因为我自己触发了一次。

Try this This worked for me in similiar condition .试试这个 这在类似的情况下对我有用。

 var aantal_tickets = 4;   //Here put the number of times you want auto submit
      (function submit(){
        if(aantal_tickets == 0) return;
        form.submit();   //action you want to perform
        aantal_tickets--;
        setTimeout(submit, 1000);   //Each second
      })(); 

Here is your solution:这是您的解决方案:

// clickedTimes=5; the value of clickedTimes must be set. 
$(document).ready(function() {      
    $('#single_add_to_cart_button').on('click', function(event){
        if ((clickedTimes-1)==0) {
                //clickedTimes=5;  .
                return;
        }
        clickedTimes -=1;
        console.log(`Click-Event triggered ${(5-(clickedTimes))} times.`);
        $( "#single_add_to_cart_button" ).trigger( "click" );
    });
})

Here is a working Fiddle这是一个工作小提琴

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

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