首页 > 解决方案 > 如何在 JS 中感知用户输入中的特定字母

问题描述

如果我输入带有字母 1 的内容,它应该重定向到另一个页面,例如:1sometxt。
如果不是,它应该重定向到另一个页面

function functionclick() {
     var input = document.getElementById("myInput").value;
     var input1 = "1";
     if (input === input1 + i dont know what to give here ) {
         window.open("www.example.com");
     }else {
window.open("www.example.com");
}
 }

标签: javascripthtml

解决方案


您可以通过使用检查是否input包含“1” input.includes('1'),或者如果“1”应该是第一个字符,则 if 条件可能是这样的

if(input[0] === input1) { // doesn't matter what next characters are, check if input first character equal to input1 variable
  // your code here
}

推荐阅读