我从另一篇帖子中获取了这段代码。
Number.prototype.countDecimals = function () {
if(Math.floor(this.valueOf()) === this.valueOf()) return 0;
return this.toString().split(".")[1].length || 0;
}
var x = 23.453453453;
x.countDecimals(); //returns 9 as expected
基本上,它返回给定数字中的小数位数。问题在于,我需要从输入框内的数字中获取该值:
console.log($('#myinput').val().countDecimals());
...但是该操作返回:未捕获的类型错误:未定义的函数
我如何将先前的代码与输入值(使用jQuery)集成在一起?谢谢!
value
?这里有一个例子:http://jsfiddle.net/5p8L84ot/ - Spencer Wieczorek