javascript - 如何在 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");
}
}
解决方案
您可以通过使用检查是否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
}
推荐阅读
- tensorflow - TensorFlow 2 中的梯度检查点
- java - android中的水平渐进式仪表
- javascript - addEventListener/querySelector/Toggle - 需要帮助更改颜色但取消选择其他选项卡
- javascript - 如何使用 javascript 在富文本编辑器上启用标题(h1、h2、h3、h4)?
- android - 错误的 NDK 有时链接不满意
- reactjs - 如何组织我的嵌套对象以获得更好的状态管理?
- java - 如何在 OSM 中的两点之间获取所有 Route 的 latlng
- python - 解析管道分隔的 CSV Python
- r - 使用 R 制作分组箱线图的麻烦
- javascript - Angular - img 在本地加载正常,但在开发服务器上加载不正常