简体   繁体   English

单击一个按钮以触发另一个按钮的延迟单击

[英]Clicking a button to trigger a delayed click for another buttons

I'm trying to make a button that when it's being a click, it will trigger a click for another button too but with a delay for each of them. 我正在尝试创建一个按钮,当它单击时,它也会触发另一个按钮的单击,但是每个按钮都有延迟。 For example, if the Main Button is clicked, the Sub Button 1 will trigger a click, then Sub Button 2 will be clicked after 2 seconds and the Sub Button 3 will be clicked after 4 seconds. 例如,如果单击“ Main Button ,则“ Sub Button 1将触发单击,然后在2秒钟后单击“ Sub Button 2并在4秒钟后单击“ Sub Button 3

The real scenario is a customer can select up to 3 products, the 3 products will be added to a cart if they click the main button because the add to cart button of those 3 products will be clicked too as they click the main button. 实际场景是客户最多可以选择3种产品,如果单击main button ,这3种产品将被添加到购物车中,因为这3种产品的“ add to cart button也将在单击主按钮时也被单击。 The products page has an Ajax. 产品页面上有一个Ajax。 If I click the main button, sometimes only 1 or 2 product(s) are being added. 如果单击主按钮,有时仅添加1或2个产品。 I'm trying to delay a click for each button. 我正在尝试延迟每个按钮的单击。

 $(".main-button").on("click",function(){ $(".container .row").each(function(i){ $rowNum = $(this).attr("id","row-" + i); $rowNum.find("button").trigger("click").delay(5000).text("clicked"); }) }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button class="main-button">Main Button</button> <div class="container"> <div class="row"> <button class="sub-button">Sub Button 1</button> </div> <div class="row"> <button class="sub-button">Sub Button 2</button> </div> <div class="row"> <button class="sub-button">Sub Button 3</button> </div> </div> 

$(".main-button").on("click",function(){
   myLoop ($(".container .row").children().length)
});


var i = 1;                    

function myLoop (count) {          
  setTimeout(function () {    
    $('.container :nth-child('+i+')').children('button').text("clicked")               
      if (i < count) {            
      i++
         myLoop(count);            

      }                       
   }, 1000)
}

Try demo - https://jsfiddle.net/jijomonkmgm/oL3bzp5r/ 尝试演示-https: //jsfiddle.net/jijomonkmgm/oL3bzp5r/

This isn't specifically what you asked for, but the outcome of the functionality that you seek will be the same. 这是没有具体你问什么,但的功能,你所寻求的结果将是相同的。

In the comments, others and myself, talked about how you could choose to call your sub button functions within the main function itself, without having the logic of a chained button clicking functionality. 在评论中,其他人和我本人谈到了如何选择在主函数本身内调用子按钮函数,而无需链接按钮单击功能的逻辑。

Before you can do that, you need to make sure that all of your sub functions are within the global scope so that you can access them in your main function. 在执行此操作之前,需要确保所有子功能都在全局范围内,以便可以在主功能中访问它们。

Example: 例:

subButtonOneFunction() {
    //do something
}

subButtonTwoFunction() {
    //do something
}

subButtonThreeFunction() {
    //do something
}

$(".main-button").on("click",function(){
    $.ajax({
        type: 'POST',
        url: '/path/to/your_page.php',
        data: {
            data : dataVar,
            moreData : moreDataVar
        },
        success: function (html) {
            subButtonOneFunction();
            subButtonTwoFunction();
            subButtonThreeFunction();
            //and so forth
        }
    })
});

The sub button functions in this example is within reach of the main function, and so you will be able to call the sub functions within the main function. 在此示例中,子按钮功能位于主功能的范围之内,因此您将能够在主功能内调用子功能。

Not knowing if there is more to your main function, other than the delayed, button clicking loop that you were attempting, I tried to provide an example of how an AJAX function works, plus adding a success function where you can call your sub functions. 除了您尝试的延迟按钮单击循环之外,我不知道主要功能是否还有更多,我尝试提供一个AJAX函数如何工作的示例,以及添加一个可以调用子函数的成功函数。

Firstly, we declare the type . 首先,我们声明type The type is the data type for the data the AJAX function will parse. 类型是AJAX函数将解析的数据的数据类型。 Notable data types are POST and GET . 值得注意的数据类型是POSTGET

Secondly, we declare the url . 其次,我们声明url The url is the page where your data will be parsed to. 网址是您的数据将被解析到的页面。 It can be your current page, or another page entirely. 它可以是您当前的页面,也可以是另一个页面。

Thirdly, we declare our variable names for our data that we wish to parse, and what their content is. 第三,我们声明我们要解析的数据的变量名及其内容。 The variables go by the same logic as any other variables that you know from JavaScript. 这些变量与您从JavaScript知道的任何其他变量具有相同的逻辑。 So they can contain numbers, strings, arrays, whatever you normally know. 因此,它们可以包含数字,字符串,数组,无论您通常知道什么。

Taking one of the data variables from the AJAX example and giving it a value could be done like this: 可以从AJAX示例中获取数据变量之一并为其赋值,如下所示:

Our AJAX example: data : dataVar 我们的AJAX示例: data : dataVar

Literal example: data : $('input#SomeInputContainingValue').val() 文字示例: data : $('input#SomeInputContainingValue').val()

our AJAX variable data will now contain the value of an input field that has the id SomeInputContainingValue . 我们的AJAX变量数据现在将包含ID为SomeInputContainingValue的输入字段的值。

Another example using the clicked elements value: data : $(this).val() 使用clicked元素值的另一个示例: data : $(this).val()

As you can see, the data is simply variables that you would declare as any other JavaScript variable. 如您所见,数据只是您将声明为任何其他JavaScript变量的变量。 The difference here is that : is basically the syntax for = in the AJAX function's data array. 此处的区别在于:基本上是AJAX函数的数据数组中=的语法。

Lastly, we declare our success function in the AJAX function. 最后,我们在AJAX函数中声明success函数。 What this does, is that it allows us to "do something" upon success. 这是因为它使我们能够在成功时“有所作为”。 This is where you can call your sub functions for instance. 例如,您可以在此处调用子函数。

This would be a much cleaner approach, and will be much easier to look through when going over the application in the future, and doesn't look like a "hack" or other workarounds. 这将是一种更清洁的方法,并且将来在遍历应用程序时将更容易浏览,并且看起来不像是“ hack”或其他变通方法。

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

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