添加.value时JavaScript querySelector不工作

添加.value时JavaScript querySelector不工作,javascript,Javascript,我正在创建一个程序,该程序通过输入键接收用户输入 我在if语句中使用userInput with.value,它工作得非常好。但当我尝试将其用作变量时,没有输出任何内容,控制台中也没有任何内容 我尝试执行querySelector(“input['name=“command”]”)以查看它是否可以工作,但同样,没有输出任何内容,控制台中也没有显示任何内容 var userInput = document.querySelector("input[name = 'command']&q

我正在创建一个程序,该程序通过输入键接收用户输入

我在if语句中使用userInput with.value,它工作得非常好。但当我尝试将其用作变量时,没有输出任何内容,控制台中也没有任何内容

我尝试执行
querySelector(“input['name=“command”]”)
以查看它是否可以工作,但同样,没有输出任何内容,控制台中也没有显示任何内容

var userInput  = document.querySelector("input[name = 'command']")
var theInput = userInput.value.toLowerCase();
var theConsole = document.querySelector("#console");
theConsole.scrollTop = theConsole.scrollHeight;
var myScroll = document.getElementById("scroll");

    function doAThing(event) {
        var theKeyCode = event.keyCode;
        
        if(theKeyCode === 13) {
        acceptCommand();
        setInterval(scrollUpdate, 1000)
        }
    }
function scrollUpdate() {
    myScroll.scrollTop = myScroll.scrollHeight;
}

function acceptCommand() {
    var p = document.createElement("p");

    if(theInput=== "help") theConsole.append("Help is given!", p);

    //using the keywords
    }
    


文件
更换div#控制台元件:

<div id="console">

对于此输入:

<input type="text" id="console">


您将希望引用
用户输入.value
而不是
输入
。因为
输入
设置为设置变量时的值,即使
用户输入
的值发生变化,它也不会得到更新。

使用==代替===可能?idk问题是您输入变量的时刻put值。显然,在加载页面时您会得到它,正确的做法是在
acceptCommand
put
var userInput=document.querySelector(“命令”)中获取值
inside
acceptCommand
@AksJacoves,这实际上不是本例中的问题。您可以声明一个输入的变量,稍后引用它并获得正确的值。事实上,除了额外的end div之外,我还测试了这段代码,它可以工作。因为您发布的代码可以工作,所以您尝试使用它所使用的变量你不工作了吗?
<input type="text" id="console">