输入框要求输入两个特定的词
input box asking for two specific words
我是新手,所以我希望我能很好地解释我的问题是什么。
我有一个测验,为了得到答案,我创建了一个输入框。要转到另一个 link,您必须在其中输入两个词,但顺序应该无关紧要。写 "word1 word2" 或 "word2 word1" 应该没有关系,应该只有一个规则:两个词都应该提到。
这可能吗?
到目前为止我的代码:
function checkText()
{
var textwunf_1 = document.getElementById("wunf").value;
if(textwunf_1.toLowerCase() == "word1" && "word2"){
window.open("URL","_self");
}
else{
xxx
}
}
没用。
之前我只想检查是否使用了一个词,像这样:
var textwunf_2 = 'word1';
function checkText()
{
var textwunf_1 = document.getElementById("wunf").value;
if(textwunf_1.toLowerCase().indexOf(textwunf_2) == -1){
xxx
}
else{
window.open("URL","_self");
}
}
这行得通,但我不能用它来表示两个词,因为如果我写
var textwunf_2 = 'word1 word2';
顺序不能是'word2 word1'...
我的问题有解决方案吗?
希望大家能理解和帮助我,谢谢!
基于 OP
的评论:
if the user types 3 words and two of them match with the answer, it should be also okay! even better if even 3 words or more are possible, as long as the user puts my two words in it..
您可以使用 if
上的两个条件检查两个词是否在文本中:
textwunf_1.toLowerCase().indexOf("word1") >= 0
AND
textwunf_1.toLowerCase().indexOf("word2") >= 0
尝试下一个代码:
var textwunf_2 = 'word1';
var textwunf_3 = 'word2';
function checkText()
{
var textwunf_1 = document.getElementById("wunf").value;
if ((textwunf_1.toLowerCase().indexOf(textwunf_2) >= 0) &&
(textwunf_1.toLowerCase().indexOf(textwunf_3) >= 0))
{
window.open("URL","_self");
}
else
{
// xxx
}
}
另一种方法:
var words = ["word1", "word2"];
function CheckWords() {
var inputWords = document.getElementById("wunf").value.split(' ');
var allWordsFound = true;
if (inputWords.length !== words.length) { return false; }
inputWords.forEach(function(word) {
if (words.indexOf(word.toLowerCase()) === -1) {
allWordsFound = false;
return;
}
});
return allWordsFound;
}
console.log(CheckWords());
我正在创建一个接收文本并检查是否包含答案(xx
和 yy
)的函数,顺序无关紧要。 ans
列表,可以有 1,2 个或更多单词,它会起作用。
let ans = ['xx','yy'];
function check(text){
text = text.toLowerCase();
let counter = 0;
ans.forEach((x) => {text.includes(x) && counter++ })
return counter === ans.length
}
console.log(check("aa bb")) // false
console.log(check("xx bb")) // false
console.log(check("aa yy")) // false
console.log(check("xx yy")) // true
console.log(check("yy xx")) // true
我是新手,所以我希望我能很好地解释我的问题是什么。
我有一个测验,为了得到答案,我创建了一个输入框。要转到另一个 link,您必须在其中输入两个词,但顺序应该无关紧要。写 "word1 word2" 或 "word2 word1" 应该没有关系,应该只有一个规则:两个词都应该提到。 这可能吗?
到目前为止我的代码:
function checkText()
{
var textwunf_1 = document.getElementById("wunf").value;
if(textwunf_1.toLowerCase() == "word1" && "word2"){
window.open("URL","_self");
}
else{
xxx
}
}
没用。
之前我只想检查是否使用了一个词,像这样:
var textwunf_2 = 'word1';
function checkText()
{
var textwunf_1 = document.getElementById("wunf").value;
if(textwunf_1.toLowerCase().indexOf(textwunf_2) == -1){
xxx
}
else{
window.open("URL","_self");
}
}
这行得通,但我不能用它来表示两个词,因为如果我写
var textwunf_2 = 'word1 word2';
顺序不能是'word2 word1'...
我的问题有解决方案吗?
希望大家能理解和帮助我,谢谢!
基于 OP
的评论:
if the user types 3 words and two of them match with the answer, it should be also okay! even better if even 3 words or more are possible, as long as the user puts my two words in it..
您可以使用 if
上的两个条件检查两个词是否在文本中:
textwunf_1.toLowerCase().indexOf("word1") >= 0
AND
textwunf_1.toLowerCase().indexOf("word2") >= 0
尝试下一个代码:
var textwunf_2 = 'word1';
var textwunf_3 = 'word2';
function checkText()
{
var textwunf_1 = document.getElementById("wunf").value;
if ((textwunf_1.toLowerCase().indexOf(textwunf_2) >= 0) &&
(textwunf_1.toLowerCase().indexOf(textwunf_3) >= 0))
{
window.open("URL","_self");
}
else
{
// xxx
}
}
另一种方法:
var words = ["word1", "word2"];
function CheckWords() {
var inputWords = document.getElementById("wunf").value.split(' ');
var allWordsFound = true;
if (inputWords.length !== words.length) { return false; }
inputWords.forEach(function(word) {
if (words.indexOf(word.toLowerCase()) === -1) {
allWordsFound = false;
return;
}
});
return allWordsFound;
}
console.log(CheckWords());
我正在创建一个接收文本并检查是否包含答案(xx
和 yy
)的函数,顺序无关紧要。 ans
列表,可以有 1,2 个或更多单词,它会起作用。
let ans = ['xx','yy'];
function check(text){
text = text.toLowerCase();
let counter = 0;
ans.forEach((x) => {text.includes(x) && counter++ })
return counter === ans.length
}
console.log(check("aa bb")) // false
console.log(check("xx bb")) // false
console.log(check("aa yy")) // false
console.log(check("xx yy")) // true
console.log(check("yy xx")) // true