有人可以帮助0.1步的限制功能,没有字符,也有最小和最大值



这是目前为止的代码以及我希望输入拒绝

的限制函数
  • 阶跃值为0.1即不小于0.1即0.01
  • 没有字符w,r,t
  • 由函数输入即1设置的最小值
  • 一个由函数输入即5设置的最大值
  • 请注意我编写其余代码的方式,输入表单的类型不能是="number"必须="text">

如有任何帮助,不胜感激

function myFunction(e, low, high) {
console.log("called");
console.log("low" + low);
console.log("high" + high);
console.log(e.target.value + e.key);
var charValue = String.fromCharCode(e.keyCode);
var nextValue = e.target.value + e.key;
if (((!/^(d+)?([.]?d{0,1})?$/.test(e.target.value + e.key)) && (e.which != 8)) && (nextValue < 1 || nextValue > 5)) {
e.preventDefault()
}
}
<!DOCTYPE html>
<html>
<form name="addtext">
SetPoint :<input id="setPoint" type="text" name="setPoint" onkeydown="myFunction(event,3, 5)" /><br />
</form>

除非你在玩Code Golf,否则把所有的逻辑都塞进一个条件里是没有好处的。

将条件拆分为多个if语句,以便您可以清晰地实现逻辑。

需要将硬编码的15替换为lowhigh

function myFunction(e, low, high) {
console.log("called");
console.log("low" + low);
console.log("high" + high);
var nextValue = e.target.value + e.key;
console.log(nextValue);
if (e.which == 8) { // allow backspace
return;
}
if (!/^(d+)?([.]?d{0,1})?$/.test(nextValue)) {
e.preventDefault(); // non-number, don't allow
}
if (nextValue < low || nextValue > high) {
e.preventDefault();
}
}
<!DOCTYPE html>
<html>
<form name="addtext">
SetPoint :<input id="setPoint" type="text" name="setPoint" onkeydown="myFunction(event,3, 5)" /><br />
</form>

或者,您可以使用为此设置的输入。不需要javascript

<input id="range" value="1" type="range" step="0.1" min="0.1" max="5.0">

最新更新