Passing a variabe to jquery using Odometer

2019-04-17 06:16发布

问题:

I would like to use a jquery odometer to display information on a master page. http://www.jqueryscript.net/animation/Smooth-Animated-Numbers-with-Javascript-CSS3-odometer.html

In order to do that I have to retrieve that value from SQL Server using C#. Then I have to pass it to the jscript odometer in the html() as shown below. If I get the valuje - how do Isend it to the javascript?

<script>
      setTimeout(function(){
        $('.odometer').html('123222');
      }, 1000);
    </script>

回答1:

You can use the below implementation to pass the value from code behind to Jquery

This is one example how to do it.

First Declare a Public Variable in code behind

//Declare a Public Variable in code behind

 public string odometervalue = "667";

Read the value in Jquery Like given below

<script>
        $(function () {
            setTimeout(function () {
                //Get the value from serverside
                var uid = '<%=odometervalue  %>';
                odometer.innerHTML = uid;
            }, 1000);
        });

    </script>


回答2:

Try this html

</head>
<body>

<script src="https://rawgit.com/HubSpot/odometer/v0.4.6/odometer.min.js"></script>
<style src="https://rawgit.com/HubSpot/odometer/master/themes/odometer-theme-default.css"></style>

<script>
  odometerOptions = { 
    auto: false 
};
</script>

<script type = "text/javascript">
  $(function(){
    var exampleOdometerValue = 123456;
    var exampleOdometer = new Odometer({ el: $('.odometer-example')[0], theme: 'digital', value: exampleOdometerValue });
    exampleOdometer.render()

    setTimeout(function(){
      exampleOdometerValue = exampleOdometerValue+100.07;
      exampleOdometer.update(exampleOdometerValue);
    }, 2000);

  });
</script>
<div class="odometer odometer-example">123</div>

</body></html>