使用一个变量来查找另一个变量?

huangapple go评论73阅读模式
英文:

Use a variable to find another variable?

问题

我一直在开发一个涉及用户通过<input>元素输入的程序,并希望根据他们输入的内容更改不同的变量,类似于以下方式:

var input = document.getElementById('myInput').value;
var a = "ac";
var b = "bc";
var c = "cc";
alert(input); // 其中 input 在运行时被输入的内容替换,所以如果输入 a,它会弹出 "ac"

显然,上面的示例不起作用,但是否有一种方法可以让它起作用?

英文:

I have been working on a program involving user input via an <input> element, and wanted to do something where depending on what they typed, it changed a different variable, with something like this:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var input = document.getElementById(&#39;myInput&#39;).value;
var a = &quot;ac&quot;;
var b = &quot;bc&quot;;
var c = &quot;cc&quot;;
alert(input)//Where input is replaced once ran with what is inputed, so if a is typed it alerts &quot;ac&quot;

<!-- language: lang-html -->

&lt;input id=&quot;myInput&quot; type=&quot;text&quot; value=&quot;a&quot; /&gt;

<!-- end snippet -->

Obviously, the above example does not work, but is there a way where it would?

答案1

得分: 1

你可以将所有变量放入一个对象中,然后根据输入动态访问其属性。

const obj = {
  a: "ac",
  b: "bc",
  c: "cc"
};
document.querySelector('button').addEventListener('click', e => {
  const val = document.getElementById('myInput').value;
  console.log(obj[val]);
});
<input id="myInput" type="text" value="a" />
<button>获取值</button>
英文:

You can put all the variables in an object instead and dynamically access its properties based on the input.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const obj = {
  a: &quot;ac&quot;,
  b: &quot;bc&quot;,
  c: &quot;cc&quot;
};
document.querySelector(&#39;button&#39;).addEventListener(&#39;click&#39;, e =&gt; {
  const val = document.getElementById(&#39;myInput&#39;).value;
  console.log(obj[val]);
});

<!-- language: lang-html -->

&lt;input id=&quot;myInput&quot; type=&quot;text&quot; value=&quot;a&quot; /&gt;
&lt;button&gt;Get value&lt;/button&gt;

<!-- end snippet -->

答案2

得分: 0

以下是您要翻译的内容:

超简单版本,但您可能需要更多处理不匹配/无匹配等情况。

这里我在输入上放了一个事件监听器,用于检查变化,然后进行比较,只有在以输入开头时才会触发警报。也可以尝试“ac”等。

function checkStartsWith(value, arrayValue) {
  return arrayValue.startsWith(value);
}
document.getElementById('myInput')
  .addEventListener('change', function(event) {
    let inputValue = this.value;
    const values = [
      "ac", "bc", "cc"
    ];
    values.forEach((element) => {
      let hasValue = checkStartsWith(inputValue, element);
      console.log(hasValue, element);
      if (hasValue) {
        alert(element);
      }
    });
  });
<input id="myInput" type="text" /> 输入一些内容

希望这对您有帮助。

英文:

Super simple version but you probably need more for mismatches/no matches etc.

Here I put an event listener on the input for change and then ran a comparison, only alerting when it started with the input. Try also "ac" etc.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function checkStartsWith(value, arrayValue) {
  return arrayValue.startsWith(value);
}
document.getElementById(&#39;myInput&#39;)
  .addEventListener(&#39;change&#39;, function(event) {
    let inputValue = this.value;
    const values = [
      &quot;ac&quot;, &quot;bc&quot;, &quot;cc&quot;
    ];
    values.forEach((element) =&gt; {
      let hasValue = checkStartsWith(inputValue, element);
      console.log(hasValue, element);
      if (hasValue) {
        alert(element);
      }
    });
  });

<!-- language: lang-html -->

&lt;input id=&quot;myInput&quot; type=&quot;text&quot; /&gt; enter something

<!-- end snippet -->

答案3

得分: 0

Solution - 添加一个事件监听器到输入字段,然后我们可以使用它来检查用户输入并显示所需的警报。

我已经将解决方案添加到这个 CodeSandbox - https://codesandbox.io/s/nostalgic-bessie-tp8vnn

英文:

Solution - Add an event listener to the input field, which we can then use to check for user input and display the required alert.

I have added the solution to this CodeSandbox - https://codesandbox.io/s/nostalgic-bessie-tp8vnn

huangapple
  • 本文由 发表于 2023年6月12日 02:03:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76451830.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定