javascript - 如何制作 JS if 语句来查看值是否不是某些字符串?
问题描述
为什么这个 JS 不起作用?目标是在文本框中输入一些没有帮助的内容,windows-xp,显式,默认或什么都没有,按回车键,并发出警报“命令不存在”。
function checkInput() {
var commandInput = document.getElementById("command-input")
if (commandInput.value !== "help", "windows-xp", "explicit", "default", "") {
alert("Command doesn't exist.");
}
event.preventDefault();
document.getElementById("command-form").reset();
}
<form id="command-form">
<input type="text" placeholder="Enter Command" id="command-input">
<input type="submit" onclick="checkInput();" style="display: none;">
</form>
解决方案
const validCommands = ["help", "windows-xp", "explicit", "default", ""];
if (!validCommands.includes(commandInput.value)) {
alert("Command doesn't exist.");
}
推荐阅读
- perl - 复制并保留以前的输出以备份转换后的 json 数据
- angular - Angular:如何解决错误 core.js:6185
- node.js - npm WARN 已弃用 tslint@6.1.2:TSLint 已被弃用,取而代之的是 ESLint
- reactive-programming - RSocket 数据库(Cassandra 或 Postgresql)性能研究
- raspberry-pi - 如何同时发送两个不同的 g 代码指令?
- swift - 为什么在某些情况下添加 Swift 协议不需要所有通常需要的方法?
- email - MailApp.sendEmail 方法无法访问正文中带有 URL 的帐户 - 消息被阻止
- ios - 如何使我的模型符合可编码并在每次更改时将其保存在本地?
- c - 使用 fork() 创建一个祖父、父和子进程
- .net - 如何使用窗口消息等捕获最大化启动事件