忽略JavaScript搜索中的大小写

时间:2020-11-12 10:10:37

标签: javascript html web

我正在构建一个简单的搜索框。我被困在如何忽略搜索功能的大小写上。我知道有一个片段可以忽略大小写,这是什么,我应该放在哪里?

我是编程的新手,图形设计师变成了开发人员。

这里有一种产品:

<input type="search" autocomplete="off" value="" placeholder="Search here..." id="search" onchange="openPage()">

<script>
  function openPage() {
    var x = document.getElementById("search").value;

    if (x === "Oyaide") {
      window.location.replace("/Products#!/oyaide/sort/manual");
    }

    if (x === "oyaide") {
      window.location.replace("/Products#!/oyaide/sort/manual");
    }

    if (x === "OYAIDE") {
      window.location.replace("/Products#!/oyaide/sort/manual");
    }

</script>

2 个答案:

答案 0 :(得分:3)

您可以在输入值和单词上同时使用String.prototype.toLowerCase

function openPage() {
        var x = document.getElementById("search").value;

        if (x.toLowerCase() === "Oyaide".toLowerCase()) {
            window.location.replace("/Products#!/oyaide/sort/manual");
        }
}

答案 1 :(得分:0)

我认为您可能希望这样的事情有多个位置可以访问

const locations = {
  "oyaide" : "/Products#!/oyaide/sort/manual",
  "something" : "/Products#!/something/sort/manual"
}  
  

window.addEventListener("load",function() {
  document.getElementById("search").addEventListener("input",function() {
    const val = this.value.toLowerCase(); // case insensitive
    const loc = locations[val]; // as soon as one exists
    if (loc) location.replace(loc); // use it
  });
});  
<input type="search" autocomplete="off" value="" placeholder="Search here..." id="search" />

相关问题