简体   繁体   English

Javascript:将输入文本框的值与 ul li 元素的值进行比较

[英]Javascript: Compare value of an input text box to the values of ul li elements

I am trying to compare the elements inside my unordered list to the value in the input textbox.我正在尝试将无序列表中的元素与输入文本框中的值进行比较。 If the value in the input text box matches a word in the unordered list I want to do something with that word in the unordered list.如果输入文本框中的值与无序列表中的一个词匹配,我想对无序列表中的那个词做一些事情。

Here is my JavaScript code:这是我的 JavaScript 代码:

function CheckWordMatch() {
   var wordList = document.getElementById("wordSearchTable");
   var wordListLi = wordList.getElementsByTagName("li");
   var inputBoxText = document.getElementById("pickedLetters");

   for (var i = 0; i < wordListLi.length; i++) {
      if (inputBoxText.value === wordListLi[i].value) {
         wordArray.style.textDecoration = "line-through";
         console.log("IT WORKS");
      }
   }
}

#wordSearchTable is the ID of the unordered list. #wordSearchTable是无序列表的 ID。
#pickedLetters is the ID of the input text box. #pickedLetters is输入文本框的 ID。

Here is the entire javascript code and html code:这是整个 javascript 代码和 html 代码:

var allCells;
var found = false;

window.onload = init;

function init() {
   document.querySelectorAll("aside h1")[0].innerHTML = wordSearchTitle;
   document.getElementById("wordTable").innerHTML = drawWordSearch(letterGrid, wordGrid);
   document.getElementById("wordList").innerHTML = showList(wordArray);
   allCells = document.querySelectorAll("table#wordSearchTable td");
   for (var i = 0; i < allCells.length; i++) {
      allCells[i].addEventListener("mousedown", highlightLetters)
      allCells[i].style.cursor = "pointer";
      allCells[i].addEventListener("mouseup", removeMouseDown)
   }
   document.getElementById("pickedLetters").addEventListener('keyup', CheckWordMatch);

   //document.getElementById("wordTable").addEventListener("mouseup", CheckWordMatch);

   console.log("Hello");
}

/*============================================================*/

function drawWordSearch(letters, words) {
   var rowSize = letters.length;
   var colSize = letters[0].length;

   var htmlCode = "<table id='wordSearchTable'>";
   htmlCode += "<caption>Word Search</caption>";

   for (var i = 0; i < rowSize; i++) {
      htmlCode += "<tr>";

      for (var j = 0; j < colSize; j++) {
         if (words[i][j] == " ") {
            htmlCode += "<td>";
         } else {
            htmlCode += "<td class='wordCell'>";
         }
         htmlCode += letters[i][j];
         htmlCode += "</td>";
      }

      htmlCode += "</tr>";
   }
   htmlCode += "</table>";

   return htmlCode;
}

function showList(list) {
   var htmlCode = "<ul id='wordSearchList'>";

   for (var i = 0; i < list.length; i++) {
      htmlCode += "<li>" + list[i] + "</li>";
   }

   htmlCode += "</ul>";

   return htmlCode;
}

function removeMouseDown(e) {
   for (var i = 0; i < allCells.length; i++) {
      allCells[i].removeEventListener("mouseenter", highlightLetters2)
   }
   CheckWordMatch();
}

function highlightLetters2(e) {
   var inputBoxValue = document.getElementById("pickedLetters");
   e.target.style.backgroundColor = "pink";
   inputBoxValue.value = inputBoxValue.value + e.target.textContent;
   for (var i = 0; i < allCells.length; i++) {
      allCells[i].addEventListener("mouseup", removeMouseDown)
   }
}

function highlightLetters(e) {
   var inputBoxValue = document.getElementById("pickedLetters");
   e.target.style.backgroundColor = "pink";
   inputBoxValue.value = e.target.textContent;
   for (var i = 0; i < allCells.length; i++) {
      allCells[i].addEventListener("mouseenter", highlightLetters2)
   }
}

function CheckWordMatch(event) {
   var inputBoxText = event.target.value;
   var wordList = document.getElementById("wordSearchTable");
   var wordListLi = wordList.getElementsByTagName("li");
   var inputBoxText = document.getElementById("pickedLetters");
   for (var i = 0; i < wordListLi.length; i++) {
      if (inputBoxText.value === wordListLi[i].textContent) {
         wordArray.style.textDecoration = "line-through";
         console.log("IT WORKS");
      }
   }
}

<!DOCTYPE html>
<html lang="en">
<head>
   
   <title>Word Search</title>
   <meta charset="utf-8" />
   <link href="p2-layout.css" rel="stylesheet"  />
   <script src="p2-makeTable.js" async></script>
   <script src="p2-wordSearch.js" async></script>  

</head>

<body>
   <section id="main">
      <header><br></header>
      <article>
         <figure id="wordTable"></figure>
         <input type="button" value="Show Solution" id="showSolution" />
      </article>
      <aside>
         <h1 id="wordSearchTitle"></h1>
         <input type="text" id="pickedLetters" readonly />
         <figure id="wordList"></figure>
      </aside>
  </section>
</body>
</html>

First you'll need to attach an event listener to the input element #pickedLetters.首先,您需要将事件侦听器附加到输入元素#pickedLetters。 I used keyup in this example but feel free to pick what suits your use-case best.我在此示例中使用了keyup ,但请随意选择最适合您的用例的内容。

document.getElementById("pickedLetters").addEventListener('keyup', CheckWordMatch);

I also fixed some of your sample code.我还修复了您的一些示例代码。 See below with comments:请参阅下面的评论:

function CheckWordMatch(event) {
    // get the value of the target which is the value of the input element.
    var inputBoxText = event.target.value;
    var wordList = document.getElementById("wordSearchTable");
    var wordListLi = wordList.getElementsByTagName("li");
    for (var i = 0; i < wordListLi.length; i++) {
        // since you want to compare the value of the input 
        // element with the value (or text) of the li element, 
        //you should use textContent or innerText.
        if (inputBoxText === wordListLi[i].textContent) {
            wordListLi[i].style.textDecoration = "line-through";
            console.log("IT WORKS");
        }
    }
}

// attach an event listener to the input element to trigger your function.
document.getElementById("pickedLetters").addEventListener('keyup', CheckWordMatch);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM