关于javascript:如何让我的“输入一个单词”脚本使用JavaScript或jQuery工作?

How to get my “input a word” script to work using JavaSscript or jQuery?

本问题已经有最佳答案,请猛点这里访问。

HTML代码:

1
2
3
4
5
6
    <form>
    Input: <input type="text" name="input" id="input">
   
    Output: <input type="text" name="output" id="output">
    <input type="submit" value="submit" onclick="yourGuess()" id="submit">
    </form>

脚本

我不知道如何让代码将输入与数组中的单词进行比较,得出正确或错误的答案并将其放入DIV(id=answer)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
function yourGuess()
{
var n = 0;
var words = ['banana', 'orange', 'apple', 'peach'];
var guess1 = document.getElementById("input").value;

//wrong answer
if (guess1 !== words) {
    document.getElementById("answer").innerHTML ="Wrong!";

}
else {
//right answer
    if (guess1 == words) {
        document.getElementById("answer").innerHTML ="Correct!";

    }
</body>


如果您使用jquery:

1
2
3
4
5
6
7
8
9
10
11
12
$('#submit').on('click', function(event) {
  event.preventDefault();
  var wordsArray = ['banana', 'orange', 'apple', 'peach'];
  var guessInput = $('#input').val();
   
  /* Check in Array */
  if($.inArray(guessInput, wordsArray) !== -1) {
    $('#answer').text('Correct!');
  } else {
    $('#answer').text('Wrong!');
  }
});
1
2
3
4
5
6
7
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
<form>
  Input:
  <input type="text" name="input" id="input">
 
  <input type="submit" value="submit" id="submit">
</form>

或者可以使用javascript数组indexof()方法

1
2
3
4
5
6
7
8
9
10
11
function yourGuess() {
  var wordsArray = ['banana', 'orange', 'apple', 'peach'];
  var guessInput = document.getElementById('input').value;
 
  /* Check indexOf() */
  if(wordsArray.indexOf(guessInput) !== -1) {
    document.getElementById('answer').innerHTML = 'Correct!';
  } else {
    document.getElementById('answer').innerHTML = 'Wrong!';
  }
}
1
2
3
4
5
6
<form>
  Input:
  <input type="text" name="input" id="input">
 
  <input type="submit" value="submit" id="submit" onclick="yourGuess()">
</form>

Because JavaScript treats 0 as loosely equal to false (i.e. 0 ==
false, but 0 !== false), to check for the presence of value within
array, you need to check if it's not equal to (or greater than) -1.


您可以使用Array.indexOf

1
2
3
4
5
6
7
var words = ['banana', 'orange', 'apple', 'peach'];
var guess1 = document.getElementById("input").value;

// Finds the index of the given word in the array
// If it returns -1, the word is not in there
// Else returns the index of the word in the array
var index = words.indexOf(guess1);

有关Array.indexOf的更多信息,请访问此处。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function yourGuess() {
  var n = 0;
  var words = ['banana', 'orange', 'apple', 'peach','banana'];
  var guess1 = document.getElementById("input").value;

  var found = words.filter(function(item) {
    return item === guess1
  });

  //wrong answer
  if (found.length <= 0) {
    document.getElementById("answer").innerHTML ="Wrong!";
  } else {
    document.getElementById("answer").innerHTML = found.length +" Correct!";
  }
}
1
2
3
4
5
6
7
8
9
<form>
  Input:
  <input type="text" name="input" id="input">
 
  Output:
  <input type="text" name="output" id="output">
 
  <input type="submit" value="submit" onclick="yourGuess()" id="submit">
</form>


变量字是数组,所以需要循环它并检查猜测值是否等于并发数组。

1
2
3
4
5
6
7
    for(var counter = 0; counter < words.length; counter++){
    if(words[counter] != guess1){
          document.getElementById("answer").innerHTML ="Wrong!";
    }else{
      document.getElementById("answer").innerHTML ="Correct";
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
correct=false
for(x=0;x<words.length;x++){

  if(guess1==words[x]){

    document.getElementById("answer").innerHTML ="Correct!";
    correct=true
    break

  }
}
if(!correct){

  document.getElementById("answer").innerHTML ="Wrong!";

}


1
2
3
4
5
6
7
8
9
10
11
12
13
function yourGuess() {
  var words = ['banana', 'orange', 'apple', 'peach'];
  var guess1 = document.getElementById("input").value;


  var answerEl = document.getElementById("answer");

  if (words.indexOf(guess1) > -1) {
      answerEl.innerHTML ="Correct!";
  } else {
      answerEl.innerHTML ="Wrong!";
  }
}


尝试在猜测和单词上使用toString()。比较两个字符串应该有效。