希望我能解释我想要什么。。。
我有两个文本框:长度和宽度
一个框中的最大值为200,而另一个框为100。目前我正在使用这个代码:
document.getElementById('Length').onchange = function(){
if(this.value > 200){
this.value = 200;
}
if(this.value < 10){
this.value = 10;
}
}
document.getElementById('Width').onchange = function(){
if(this.value > 100){
this.value = 100;
}
if(this.value < 20){
this.value = 20;
}
}
这是棘手的部分:我希望不要设置哪个框具有哪个最大值。这应该取决于客户的类型。如果有人键入的"长度"值小于100,则"宽度"的最大值为200。但如果有人在"长度"中键入"158",则"宽度"的最大值应为100,"长度"的最大为200。反之亦然。
此外,如果有人先键入150的长度,然后键入160的宽度,则250的长度应更改为100,因此200的最大值应属于最近值高于100的框。
编辑:此外,是否可以将最大值与最小值连接?因此,得到200最大值的盒子也有10最小值,然后另一个盒子有100最大值和20最小值。希望这是可以理解的。
可能是这样的:
var length = document.getElementById('Length'),
width = document.getElementById('Width');
length.onchange = function() {
var maxValue = width.value < 100 ? 200 : 100;
if (this.value > maxValue) {
this.value = maxValue;
}
if (this.value < 10) {
this.value = 10;
}
};
width.onchange = function() {
var maxValue = length.value < 100 ? 200 : 100;
if (this.value > maxValue) {
this.value = maxValue;
}
if (this.value < 10) {
this.value = 10;
}
};
jsFiddle。