首页 > 解决方案 > 更改为区分大小写时的 if 语句错误

问题描述

我创建了代码来检查输入搜索框中的特定值。如果是,网络将专注于搜索到的元素。

我的html

<input type="search" id="search-box" placeholder="Search">
<button type="submit" id="searchbar" onclick="Focus()"</button>Search</button>

我的 JavaScript


function Focus(){
    var search = document.getElementById("search-box").value;
    

    if(search == "about"){
            document.getElementById("about").focus();
        }
        else{
            alert("Not Found");
        }
}

这是有效的,但是如果我更改为 this if(search == "about" || “About”) 然后else 语句不起作用。

是否有任何其他方法可以区分大小写,例如About 、AbOut、aBoUT 等都是平等的

标签: javascriptif-statementcase-sensitive

解决方案


您可以使用search.toLowerCase() == "about"它来查看它是否是不区分大小写的匹配。确保使用正确的引号。正如您在某些情况下使用了花引号一样。


推荐阅读