我正在使用HTML
聊天机器人,我需要一种方法来检查put中的用户,看看它是否在数组中。我已经尝试过,但它没有看到工作。我需要它来让用户问候并将其存储到一个数组中。然后,当用户输入数组中的任何一个问候语以使用数组中的随机值进行回复时。
var greet = [];
var questions = [];
var responses=[];
function greets(){
var learn = document.getElementById("greets").value;
if (learn == "finished"){
document.cookie = 'greetings='+greet+'; expires=Wed, 1 Jan 2070 13:47:11 UTC; path=/'
}else{
greet.push(learn);
};
};
function ques(){
var learn = document.getElementById("ques").value;
if (learn == "finished"){
document.cookie = 'questions='+questions+'; expires=Wed, 1 Jan 2070 13:47:11 UTC; path=/'
}else{
questions.push(learn);
};
};
function res(){
var learn = document.getElementById("res").value;
if (learn == "finished"){
document.cookie = 'responses='+responses+'; expires=Wed, 1 Jan 2070 13:47:11 UTC; path=/'
}else{
responses.push(learn);
};
};
var types = [{
key: 'greet',
data: greet
}, {
key: 'questions',
data: questions
}, {
key: 'responses',
data: responses
}];
function send(){
/* isExist method two arguements
arr - array of values.
str - string to be search.
*/
var msg = document.getElementById("msg");
if(types.indexOf( msg )>=0){
console.log("YES");
} else {
console.log("NO");
}
};

<!DOCTYPE html>
<html>
<head>
<script src="index.js"></script>
<div>
<p class="user" id="user">User: </p>
<p class="bot" id="bot">Bot: </p>
</div>
<input class="talk" id="msg" placeholder="Start Talking!"></input><button class="send" onclick="send()">Send</button>
<p>Once Finished Type Finished.</p>
<p>Enter Some Greetings.</p>
<input placeholder="Enter Here, Friend!" id="greets"></input>
<button onclick="greets()">Enter Greet</button>
<br>
<p>Enter Some Question You Would Normally Ask.</p>
<input placeholder="Enter Here, Friend!" id="ques"></input>
<button onclick="ques()">Enter question</button>
<br>
<p>Enter Some Responses.</p>
<input placeholder="Enter Here, Friend!" id="res"></input>
<button onclick="res()">Enter Responses</button>
<br />
<style>
.talk{
float: right;
width: 750px;
}
div{
border: 1px solid black;
float: right;
width: 800px;
height: 250px;
}
.send{
float:right;
background-color: lightblue;
border-radius: 2px;
border: 2px solid black
}
</style>
<button onclick="loadgreets()">Load Files</button>
</head>
<body>
</body>
</html>
&#13;
答案 0 :(得分:0)
看起来您需要进行2次更改:
首先,var msg = document.getElementById("msg")
应为var msg = document.getElementById("msg").value
以获取输入的值,而不是元素本身
其次,如果msg与if(types.indexOf( msg )>=0)
中的一个对象完全匹配,types
将起作用。但是,由于types
是一个对象数组,并且您希望匹配key
,因此您可以执行此问题中建议的内容:Find a value in an array of objects in Javascript
function contains(msg, types){
for (var i=0; i < types.length; i++) {
if (types[i].key === msg) {
return true;
}
}
return false;
}
if(contains(msg, types){
console.log("YES");
} else {
console.log("NO");
}
答案 1 :(得分:0)
不确定你打算做什么,但也许我会帮你解决这个问题:
获取msg值使用document.getElementById("msg")
.value ;
检查是否存在问候语:
for(var key in greet) {
if(greet[key] == msg) {
//do a response
break; //no more loops are needed if we found a match
}
}
indexOf不支持数组,而javascript中没有像PHP的in_array那样的本机函数。