javascript - 更改为区分大小写时的 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 等都是平等的
解决方案
您可以使用search.toLowerCase() == "about"
它来查看它是否是不区分大小写的匹配。确保使用正确的引号。正如您在某些情况下使用了花引号一样。
推荐阅读
- ruby-on-rails - 为什么在进行 Postgres 查询时,rails 不在“NaN”周围加上引号?
- python - 如何将我的 Python 脚本传递到 Slurm sbatch?
- fortran - 字符声明表
- c# - 在 Azure 认知搜索中处理单复数搜索词
- junit - 使用条件制作自定义类范围的 JUnit 注释
- directx - DirectX11可以将负值写入纹理格式化的浮点数吗?
- azure-devops - 链接到 KeyVault 的 Azure DevOps 变量组的“全选”选项
- excel - 遍历列,如果值为 1,则从上面复制并粘贴
- ios - apns-collapse-id (iOS) 如何与 Firebase 一起使用?
- jquery - jquery 遍历 div 的正确顺序不起作用?