首页 > 解决方案 > jQuery:使用输入的值作为匹配数组的名称

问题描述

我正在尝试使用输入中输入的值作为我要查询的数组的名称。

这是我的输入:

<input id="enter" type="text">

这就是我现在所拥有的。

  $('#enter').on('keyup', function (e) {
    var ex01 = ['up', 'down', 'left']
    var ex02 = ['blue', 'green', 'red']
    if (e.keyCode === 13) {
      var matchingArray = 'ex' + ($(this).val());
      var firstValue = matchingArray[0];
      var secondValue = matchingArray[1];
      console.log(firstValue);
      console.log(secondValue);
    }
  });

我的意图是,如果输入“01”,它将返回 ex01 中的第一个和第二个值:

up
down

相反,我从字符串“ex01”中获取第一个和第二个字符。

e
x

标签: jqueryarraysvariables

解决方案


推荐阅读