如果html输入值在数组中,则为Javascript

时间:2022-11-16 07:41:05

I am working on a HTML chatbot and I need a way to check the users in put to see if it's in an array. I have tried already but it doesn't see to be working. I need it to take the users greets and store it into an array. Then, when the user enters any one of the greets in the array to reply with a random value from the array.

我正在研究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>

2 个解决方案

#1


0  

It looks like you need to make 2 changes:

看起来您需要进行2次更改:

First, var msg = document.getElementById("msg") should be var msg = document.getElementById("msg").value to get the value of the input, not the element itself

首先,var msg = document.getElementById(“msg”)应该是var msg = document.getElementById(“msg”)。value来获取输入的值,而不是元素本身

Second, if(types.indexOf( msg )>=0) would work if msg was an exact match to one of the objects in types. However, because types is an array of objects, and it looks like you want to match the key, you can do something like what's suggested in this question: Find a value in an array of objects in Javascript

其次,如果msg与类型中的某个对象完全匹配,则if(types.indexOf(msg)> = 0)将起作用。但是,因为types是一个对象数组,看起来你想要匹配键,你可以做类似于这个问题的建议:在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");
}

#2


0  

not sure what are you going to do, but maybe I will help you with this advices:

不确定你打算做什么,但也许我会帮你解决这个问题:

to get the msg value use document.getElementById("msg").value;

获取msg值使用document.getElementById(“msg”)。value;

to check if greeting exists you should use:

要检查是否存在问候语,您应该使用:

for(var key in greet) {
    if(greet[key] == msg) {
        //do a response
        break; //no more loops are needed if we found a match
    }
}

indexOf is not working on arrays and in javascript is no native function like PHP's in_array.

indexOf不用于数组,而javascript中没有像PHP的in_array那样的本机函数。

#1


0  

It looks like you need to make 2 changes:

看起来您需要进行2次更改:

First, var msg = document.getElementById("msg") should be var msg = document.getElementById("msg").value to get the value of the input, not the element itself

首先,var msg = document.getElementById(“msg”)应该是var msg = document.getElementById(“msg”)。value来获取输入的值,而不是元素本身

Second, if(types.indexOf( msg )>=0) would work if msg was an exact match to one of the objects in types. However, because types is an array of objects, and it looks like you want to match the key, you can do something like what's suggested in this question: Find a value in an array of objects in Javascript

其次,如果msg与类型中的某个对象完全匹配,则if(types.indexOf(msg)> = 0)将起作用。但是,因为types是一个对象数组,看起来你想要匹配键,你可以做类似于这个问题的建议:在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");
}

#2


0  

not sure what are you going to do, but maybe I will help you with this advices:

不确定你打算做什么,但也许我会帮你解决这个问题:

to get the msg value use document.getElementById("msg").value;

获取msg值使用document.getElementById(“msg”)。value;

to check if greeting exists you should use:

要检查是否存在问候语,您应该使用:

for(var key in greet) {
    if(greet[key] == msg) {
        //do a response
        break; //no more loops are needed if we found a match
    }
}

indexOf is not working on arrays and in javascript is no native function like PHP's in_array.

indexOf不用于数组,而javascript中没有像PHP的in_array那样的本机函数。