Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为聊天机器人执行代码时出现问题_Javascript_Html - Fatal编程技术网

Javascript 为聊天机器人执行代码时出现问题

Javascript 为聊天机器人执行代码时出现问题,javascript,html,Javascript,Html,我正在尝试创建聊天机器人。当我运行这段代码时,我在第11行得到一个错误:“TypeError:null不是对象(计算'document.getElementById(“message”).value')”。当代码运行时,用户应该能够输入“hi”并让计算机响应。有人能告诉我11号线出了什么问题吗?代码如下: <html> <!--Create text box for player to type in--> <input type="text" id=“mess

我正在尝试创建聊天机器人。当我运行这段代码时,我在第11行得到一个错误:“TypeError:null不是对象(计算'document.getElementById(“message”).value')”。当代码运行时,用户应该能够输入“hi”并让计算机响应。有人能告诉我11号线出了什么问题吗?代码如下:

<html>
<!--Create text box for player to type in-->
<input type="text" id=“messageâ€>
<!--Create paragraph to show chatbot's messages-->
<p id="chatbotText"></p>
<!--Create button to send messages to chatbot-->
<button onclick="input()">Send</button>
<script>
//function to process user messages
var input = function() {
    var message = document.getElementById("message").value;
    //create array with list of phrases to use in response to "Hello"
    var helloMessages = ["Hello.", "Hi.", "Hello!", "Hi!", "Hello. How are you doing?", "Hi. How are you?"];
    //function to pick random phrase
    function randomWord(arr) {
        return arr[Math.floor(Math.random() * arr.length)];
    }
    if(message === "hi" || message === "hello" || message === "hi!" || message === "hello!" || message === "hi." || message === "hello." || message === "hey." || message === "hey" || message === "hey!") {
        document.getElementById('chatbotText').innerHTML = "<p>randomWord(helloMessages)</p>";
    }
}
</script>
</html>

发送 //处理用户消息的函数 var输入=函数(){ var message=document.getElementById(“message”).value; //创建包含短语列表的数组,用于响应“Hello” var helloMessages=[“你好”,“你好”,“你好!”,“你好!”,“你好,你好吗?”,“你好,你好吗?”]; //函数来选择随机短语 函数随机字(arr){ 返回arr[Math.floor(Math.random()*arr.length)]; } 如果(消息==“你好”| |消息==“你好”| |消息==“你好!”| |消息==“你好!”| |消息==“你好。”| |消息==“你好。”| |消息==“嘿”|消息==“嘿”| |消息==“嘿!”){ document.getElementById('chatbotText').innerHTML=“randomWord(helloMessages)

”; } }
我对您的代码做了一些细微的更改,效果良好。我将按钮更改为具有ID,并从JavaScript引发事件

//处理用户消息的函数
var按钮=document.getElementById(“btnSubmit”)
button.onclick=函数(){
var message=document.getElementById(“message”).value;
//创建包含短语列表的数组,用于响应“Hello”
var helloMessages=[“你好”,“你好”,“你好!”,“你好!”,“你好,你好吗?”,“你好,你好吗?”];
//函数来选择随机短语
函数随机字(arr){
返回arr[Math.floor(Math.random()*arr.length)];
}
如果(消息==“你好”| |消息==“你好”| |消息==“你好!”| |消息==“你好!”| |消息==“你好。”| |消息==“你好。”| |消息==“嘿”|消息==“嘿”| |消息==“嘿!”){
document.getElementById('chatbotText').innerHTML=“randomWord(helloMessages)

”; } }


发送
非常感谢!我一直在努力解决这个问题,终于解决了。现在我可以继续在不同消息的响应中编程。