[英]Ignore case in javascript search
我正在構建一個簡單的搜索框。 我被困在如何在搜索功能上忽略大小寫。 我知道有一個片段可以忽略大小寫,它是什么以及我把它放在哪里?
我是編程新手,平面設計師變成了開發人員。
這是一種產品的一點:
<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>
您可以在輸入值和單詞上使用String.prototype.toLowerCase
:
function openPage() {
var x = document.getElementById("search").value;
if (x.toLowerCase() === "Oyaide".toLowerCase()) {
window.location.replace("/Products#!/oyaide/sort/manual");
}
}
我想你可能想要這樣的東西有多個位置去
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" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.