如何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;
});
答案 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)
您分享的小提琴有两个问题:
mefedron.val();
,mefedron
具有您正在寻找的价值。所以基本上,只需将你的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));