I got this HTML:
我有这个HTML:
<input type="number" name="eingangstuer_hoehe" min="70" max="200">
And this is the jQuery/javascript:
这是jQuery / javascript:
$("input").change(function(e) {
var $this = $(this);
var val = $this.val();
var max = $this.attr("max");
var min = $this.attr("min");
if(val > min || val < max)
{
}
else
{
if (val > max){
e.preventDefault();
$this.val(max);
}
if (val < min)
{
e.preventDefault();
$this.val(min);
}
}
});
Typing 1
or 2
will not change anything. Typing 3
- 69
will work how it supposed to. Typing everything above 199
(yes also 200
) will change the value to 70
..
键入1或2不会改变任何东西。键入3 - 69将如何工作。输入199以上的所有内容(是的也是200)会将值更改为70 ..
It also won't change the values above 600
它也不会改变600以上的值
2 个解决方案
#1
2
You should use the condition as shown in the code snippet below.
您应该使用下面的代码段中显示的条件。
Also You're compering string and not numbers. try parsing the values to integer with parseInt
你也在筹码而不是数字。尝试使用parseInt将值解析为整数
your code should look like this:
您的代码应如下所示:
$("input").change(function(e) {
var $this = $(this);
var val = parseInt($this.val());
var max = parseInt($this.attr("max"));
var min = parseInt($this.attr("min"));
if(val < min || val > max){
e.preventDefault();
$this.val(max);
}
}
});
#2
0
Wrong condition found. Change if(val > min || val < max)
to if(val >= min && val <= max)
发现错误的情况。将if(val> min || val
#1
2
You should use the condition as shown in the code snippet below.
您应该使用下面的代码段中显示的条件。
Also You're compering string and not numbers. try parsing the values to integer with parseInt
你也在筹码而不是数字。尝试使用parseInt将值解析为整数
your code should look like this:
您的代码应如下所示:
$("input").change(function(e) {
var $this = $(this);
var val = parseInt($this.val());
var max = parseInt($this.attr("max"));
var min = parseInt($this.attr("min"));
if(val < min || val > max){
e.preventDefault();
$this.val(max);
}
}
});
#2
0
Wrong condition found. Change if(val > min || val < max)
to if(val >= min && val <= max)
发现错误的情况。将if(val> min || val