【问题标题】:Limiting number of characters in text input [duplicate]限制文本输入中的字符数[重复]
【发布时间】:2021-02-24 07:14:24
【问题描述】:

在这里我做了一个文本输入,显示 1000 由文本范围的值划分:

<input type="range" name="rangeInput" min="50" max="200" onchange="updateTextInput(this.value);"><br>
<input type="text" id="fps" placeholder="fps" ><br>

<script>
function updateTextInput(val) {document.getElementById('fps').value=1000/val;}
</script>

数字结果显示超出小数点。如何限制文本输入中出现的字符数(例如 4 位:12.34)? maxlength 似乎在这里不起作用。

【问题讨论】:

标签: javascript html


【解决方案1】:

.toFixed(x) 方法有效,x=2 将限制为 2 位小数。

f = 12.456218;
a = f.toFixed(2);
console.log(a); // 12.45

如果您不仅想限制点后的小数位数,还想限制总位数,则必须将类型转换为字符串,然后再进行处理

【讨论】:

  • you will have to typecast to string, .toFixed() 已经返回一个字符串
  • 我的意思是在非 toFixed() 号码上
猜你喜欢
  • 2017-01-23
  • 1970-01-01
  • 2012-07-11
  • 2014-10-14
  • 2011-07-28
  • 2012-02-11
  • 1970-01-01
  • 2015-01-31
  • 1970-01-01
相关资源
最近更新 更多