如何在HTML中按下按钮后将进度条(使用Ladda)与按钮绑定?

时间:2015-01-26 03:30:23

标签: javascript jquery html css progress-bar

我有一个进度条和一个按钮点击。现在,进度条未与按钮链接。所以我该如何确保:当按钮点击时,进度条开始;当从服务器检索结果时,进度条会停止吗?一些人可以帮忙吗?

我想尝试将一些隐藏字段作为标记来通知进度条,但还没来得及尝试。

HTML按钮在这里:

<section class="progress-demo">
            <button id="sendRequest_1" onclick="SendRequest(1)" type="button" class="btn btn-danger ladda-button" data-style="expand-left"><span class="ladda-label">Send</span></button></section>

进度条正在使用

<script src="js/spin.min.js"></script><script src="js/ladda.min.js"></script>

进度条在这里:

      <script>
        // Bind normal buttons
       Ladda.bind( 'section:not(.progress-demo) button', { timeout: 2500 } );

        //Ladda.bind( 'input[type=submit]' );

        // Bind progress buttons and simulate loading progress
        Ladda.bind( 'section.progress-demo button', {
            callback: function( instance ) {
                var progress = 0;
                var interval = setInterval( function() {
                    progress = Math.min( progress + Math.random() * 0.1, 1 );
                    instance.setProgress( progress );
                    if( progress === 1 ) {
                        instance.stop();
                        clearInterval( interval );
                    }
                }, 200 );
            }
        } ); 

按钮点击功能就在这里。

function SendRequest(i){
alert("button clicked!");
//call post to get server response
... say 1s to get response
//how to bind it to progress bar when server response is back??

}

1 个答案:

答案 0 :(得分:0)

...
var l = Ladda.bind( 'section:not(.progress-demo) button', { timeout: 2500 } );
...

function SendRequest(i){
  $.ajax({
    url: "some.php",
    success: function(data){
      l.stop();
    }
  });
}