在任何数字输入上显示数据

时间:2018-08-30 07:13:11

标签: javascript jquery

我获得了适用于预定义数字(100200)的代码,但我想使其适用于任何随机的6位数字。输出保持不变。

当用户输入从000000到999999的任意6位代码时,我想以给定的格式显示英格兰伦敦的输出。

我怎么知道的?

const $main = $('#zipcode');
const $inputs = $('.location');
const values = {
  trigger: '100200',
  birds: ['London', 'England']
};
$main.on('keyup', function() {
  if (this.value === values.trigger) {
    $inputs.each(function(i, el) {
      el.value = values.birds[i];
    });
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<input type="number" id="zipcode" class="form-control" required>
<hr />
<input type="text" id="location1" class="location" required>
<input type="text" id="location2" class="location" required>

6 个答案:

答案 0 :(得分:2)

您只需将this.value === values.trigger替换为this.value.toString().length === 6

这将检查输入数量是否等于6,而不考虑实际输入的内容。

const $main = $('#zipcode');
const $inputs = $('.location');
const values = {
  trigger: '100200',
  birds: ['London', 'England']
};
$main.on('keyup', function() {
  if (this.value.toString().length === 6) {
    $inputs.each(function(i, el) {
      el.value = values.birds[i];
    });
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<input type="number" id="zipcode" class="form-control" required>
<hr />
<input type="text" id="location1" class="location" required>
<input type="text" id="location2" class="location" required>

答案 1 :(得分:1)

const $main = $('#zipcode');
const $inputs = $('.location');
const values = {
  trigger: '100200',
  birds: ['London', 'England']
};
$main.on('keyup', function() {
  if (this.value.length >= 6 && this.value >=0 && this.value <= 999999) {
    $inputs.each(function(i, el) {
      el.value = values.birds[i];
    });
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<input type="number" id="zipcode" class="form-control" required>
<hr />
<input type="text" id="location1" class="location" required>
<input type="text" id="location2" class="location" required>

答案 2 :(得分:1)

u可以将条件更改为

if (this.value.toString().length === 6) 

它只是检查数字是否有6位数字。

答案 3 :(得分:1)

我们可以在修剪输入值后检查它的长度(只是为了避免多余的空格),并验证输入是否为整数值。

const $main = $('#zipcode');
const $inputs = $('.location');
const values = {
  trigger: '100200',
  birds: ['London', 'England']
};
$main.on('keyup', function() {
  var thisValue = (this.value).trim();
  if (thisValue.length == 6 && !isNaN(thisValue)) {
    $inputs.each(function(i, el) {
      el.value = values.birds[i];
    });
  }
});

答案 4 :(得分:0)

在您的if条件=

中尝试
if (this.value >= 0 || this.value <=999999) {

答案 5 :(得分:0)

尝试此代码:

const $main = $('#zipcode');
const $inputs = $('.location');
const values = {
  trigger: '100200',
  birds: ['London', 'England']
};
$main.on('keyup', function() {
    if (Number.isInteger(this.value)) {
        if(this.value>=000000 && this.value<= 999999){
            $inputs.each(function(i, el) {
            el.value = values.birds[i];
            });
        }    
    }
});