【问题标题】:How can I execute an action if either input a or b is correct/true? [duplicate]如果输入 a 或 b 正确/正确,我该如何执行操作? [复制]
【发布时间】:2020-03-25 04:11:51
【问题描述】:

用户将“足球”写入我的输入栏,然后执行第 6 行和第 7 行的操作。
但是如果用户用大写字母写“足球”怎​​么办? 我试图实现“逻辑或”||但我无法让它工作。

var input = document.getElementById("user_input");
let footballTeams = ["Manchester","Barcelona","Copenhagen"]

if (input.value == "football") {
    alert("These cities have football teams:" +
          "\n" + footballTeams[0] + "," + " " + footballTeams[1] + "," + " " + footballTeams[2])

【问题讨论】:

  • 比较时可以使用input.value.toLowerCase()
  • input.value.toLowerCase() === "足球"
  • 只需将您的输入标准化为标准,就像所有小写字符一样并检查它。

标签: javascript arrays logical-operators


【解决方案1】:

试试这个:

if (input.value.toLowerCase() == "football")

【讨论】:

    【解决方案2】:

    试试

    if (input.value.toLowerCase() == "football") {
       console.log('inside if');
    }
    <input id=input value="Football">

    【讨论】:

    • 请至少努力解释发生了什么。
    • 立即工作!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    相关资源
    最近更新 更多