javascript - 当我在输入中询问时,如何让 Javascript 显示当前时间?

标签 javascript input

我想查看在输入中输入某些特定文本时的当前时间,但它不起作用

HTML

        <form>
            <input type="text" id="inputcommand"></input>
            <input type="button" value="Submit" onclick="showTime()"></input>
        </form>
            <p id="output">Output here</p>

Javascript

var command = document.getElementById("inputcommand");
var output = document.getElementById("output");
var showTime = function(){
var askForTime = ["What time is it?","Show me time","Time"];
if(command.value === askForTime){
    output.innerHTML = Date();
}
};

最佳答案

使用for循环迭代数组值askForTime

var command = document.getElementById("inputcommand");
var output = document.getElementById("output");
var showTime = function(){
var askForTime = ["What time is it?","Show me time","Time"];

for(var i=0; i<askForTime.length; i++)
if(command.value == askForTime[i]){
    output.innerHTML = Date();
}
};

关于javascript - 当我在输入中询问时,如何让 Javascript 显示当前时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30693140/

相关文章:

javascript - 单击以关闭菜单 - 多个实例

javascript - 读取 HTML block 表单变量,查找带有 id 的特定标签元素,以及在哪里查找标签插入文本!用 jQuery?

PHP 输入 GET 变量清理

javascript - 使用 AngularJS 使用复选框过滤对象属性

javascript - 为什么 console.log(true, '\t' ) 打印 true " "?

带有 gmap 和 ionic 框架的 Javascript 代码

c - 来自标准输入的 read() 不会忽略换行符

javascript - 输入框在 backspace/del 上不能正常工作

c - 从 shell 脚本向 C 程序发送输入

javascript - 多个输入值相加或相减总计