dongqixuan3112 2011-06-21 20:13
浏览 21
已采纳

两个框,两个最大值 - 但未设置哪个值属于哪个框

Hopefully I can explain what I'm looking for...

I've got two text boxes: Length and Width

In one box the max value is 200 while in the other it's 100. At the moment I'm using this code:

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;
      }
}

Here's the tricky part: I would like it to not be set which box has which max value. That should depend on what the customer types. If someone types a value of under 100 in Length, then Width's max is the 200. But if someone types "158" in Length then Width's max should be the 100 and Length's max is the 200. And Vice versa.

Also, if someone first types 150 in Length and then types 160 in Width, then the 250 in Length should be changed to 100 - so the 200 max value should belong to the box that's most recently has a value higher than the 100 max.

Edit: Also, would it be possible to have the max values to be connected with a minimum value? So the box that gets the 200 max also has a 10 minimum and then the other box has the 100 max and the 20 minimum. Hope that's understandable.

  • 写回答

1条回答 默认 最新

  • dousui7410 2011-06-21 20:17
    关注

    Something like this, perhaps:

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画