如何在按钮点击时获取并输出里程表的当前值?

时间:2017-08-22 23:58:22

标签: javascript jquery

如何1)获得和2)按钮点击输出里程表的当前值?应该执行此操作的snipet位于JavaScript的底部,但它不起作用。

我的代码:https://jsfiddle.net/aht87opr/8/

里程表的文档可能派上用场: http://brock-family.org/gavin/software/web/odometer.html(其中"从里程表获取当前值:val = myOdometer-> get();")

//My attempt to output the current value from the odometer:
$('button').click(function() {
   n = myOdometer.get();
   return n;
});

4 个答案:

答案 0 :(得分:4)

您应该只使用myOdometer.get(),它已经为您提供了所需的号码。

如果要将值放在#value元素中,可以使用以下内容:

$('button').click(function() {
    var mefedron = myOdometer.get();
    $('#value').text(mefedron);
});

以下是你的jsfiddle中的修复:
https://jsfiddle.net/aht87opr/14/

答案 1 :(得分:1)

这是一个工作小提琴 - https://jsfiddle.net/6abu3ruf/(你必须修复CSS)

您分享的小提琴有两个问题:

  1. 您不必mefedron.val();mefedron具有您正在寻找的价值。
  2. 您没有在任何地方设置该值。
  3. 所以基本上,只需将你的onclick更改为:

    $('button').click(function() {
       var mefedron = myOdometer.get(n);
       $("#value").text(mefedron);
    });
    

答案 2 :(得分:0)

只需使用以下内容更新您的按钮事件处理程序:

$('button').click(function() {
     var mefedron = myOdometer.get(n);
      mefedron=  Math.round(mefedron * 100) / 100
      $("#value").text(mefedron)


    });

以下是jsFiddle jsFiddle

上托管的代码的工作版本

答案 3 :(得分:0)

我尝试了您的代码,confirm(Math.floor(mefedron*10));可以使用。

$('button').click(function() {
      var mefedron = myOdometer.get(n);
      confirm(Math.floor(mefedron*10)); // it works
      function number() {
      return mefedron.val();
        }
        number();

    });

所以,如果您只想返回里程表,那么点击按钮事件就会返回Math.floor(mefedron*10);

要在div#value,$("div#value").html(Math.floor(mefedron*10));

上显示它