有人可以协助限制 0.1 步的功能,没有字符,还有最小值和最大值
Could someone please assist with the limiting functions of a 0.1 step and no characters and also a min and max value
这是我目前的代码以及我希望输入拒绝的限制函数
- 步长值为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
语句,这样您就可以清楚地实现您的逻辑。
您需要将硬编码的 1
和 5
替换为 low
和 high
。
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">
这是我目前的代码以及我希望输入拒绝的限制函数
- 步长值为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
语句,这样您就可以清楚地实现您的逻辑。
您需要将硬编码的 1
和 5
替换为 low
和 high
。
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">