用于从下拉列表选项中搜索表格的 Javascript 函数

我有一个混合了“简单”、“中等”和“困难”的问题表。我希望用户能够从下拉列表中选择的内容中按难度和主题进行搜索。这是我的下拉搜索代码:


<div class="search">


  <label for="dflag"> Difficulty: </label>

  <select id="dflag" name="dflag">

      <option value="">Select</option>

      <option value="0">Easy</option>

      <option value="1">Medium</option>

      <option value="2">Hard</option>


      </select>

</div>

<div class="search">

  <label for="tflag"> Topic: </label>

  <select id="tflag" name="tflag">

        <option value="">Select</option>

        <option value="A">Arrays</option>

        <option value="S">Strings</option>

        <option value="C">Conditionals</option>

        <option value="L">Loops</option>

       </select>

</div>`<button type="submit" onclick="select()"> Submit </button>`

我是 javascript 新手,所以这是我到目前为止所尝试过的:


<script>

  function select(){

  var input = document.getElementById('dflag').value;

  var filter = input.value;

  table = document.getElementById("question_table");

  if dflag.value == 'Easy' {

    display.getElementsByClassName('easy');

  }

  else if dflag.value== 'Medium' {

    display.getElementsByClassName('medium');

  }

  else {

    display.getElementsByClassName('hard');

  }


}

</script>

我不能使用任何库,所以没有jquery。有什么想法吗?


德玛西亚99
浏览 122回答 1
1回答

慕尼黑8549860

你应该给出简单、中等、困难的值,然后用它的值过滤&nbsp;function select(){&nbsp; var input = document.getElementById('dflag').value;&nbsp; &nbsp; document.querySelectorAll('tr').forEach(element=>{&nbsp; &nbsp; &nbsp; element.style.display = "table-row";&nbsp; &nbsp; })&nbsp;&nbsp; &nbsp; document.querySelectorAll('tr:not(.'+input+')').forEach(element=>{&nbsp; &nbsp; &nbsp; element .style.display = "none";&nbsp; &nbsp; })}<div class="search">&nbsp; <label for="dflag"> Difficulty: </label>&nbsp; <select id="dflag" name="dflag"onchange="select()">&nbsp; &nbsp; &nbsp; <option value="">Select</option>&nbsp; &nbsp; &nbsp; <option value="easy">Easy</option>&nbsp; &nbsp; &nbsp; <option value="medium">Medium</option>&nbsp; &nbsp; &nbsp; <option value="hard">Hard</option>&nbsp; &nbsp; &nbsp; </select></div><div class="search">&nbsp; <label for="tflag"> Topic: </label>&nbsp; <select id="tflag" name="tflag">&nbsp; &nbsp; &nbsp; &nbsp; <option value="">Select</option>&nbsp; &nbsp; &nbsp; &nbsp; <option value="A">Arrays</option>&nbsp; &nbsp; &nbsp; &nbsp; <option value="S">Strings</option>&nbsp; &nbsp; &nbsp; &nbsp; <option value="C">Conditionals</option>&nbsp; &nbsp; &nbsp; &nbsp; <option value="L">Loops</option>&nbsp; &nbsp; &nbsp; &nbsp;</select></div>`<button type="submit" onclick="select()"> Submit </button>`<div class="input-group mb-3">&nbsp; &nbsp; &nbsp; <form>&nbsp; &nbsp; &nbsp; &nbsp; <table class="uk-table" id="question_table">&nbsp; &nbsp; <tr class="header">&nbsp; &nbsp; &nbsp; &nbsp; <th>Topic</th>&nbsp; &nbsp; &nbsp; &nbsp; <th>Difficulty</th>&nbsp; &nbsp; &nbsp; &nbsp; <th>Name</th>&nbsp; &nbsp; &nbsp; &nbsp; <th>Question</th>&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr class="medium">&nbsp; &nbsp; &nbsp; &nbsp; <td>Strings</td>&nbsp; &nbsp; &nbsp; &nbsp; <td>Medium</td>&nbsp; &nbsp; &nbsp; &nbsp; <td>Not</td>&nbsp; &nbsp; &nbsp; &nbsp; <td >Write a function named not_string that takes argument str and adds 'not' to the front of the given string, unless the string already begins with 'not' then return the string unchanged.</td>&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr class="easy">&nbsp; &nbsp; &nbsp; &nbsp; <td>Conditionals</td>&nbsp; &nbsp; &nbsp; &nbsp; <td>Easy</td>&nbsp; &nbsp; &nbsp; &nbsp; <td>hello</td>&nbsp; &nbsp; &nbsp; &nbsp; <td >Write a function named hello_there that takes parameters a_hello and b_hello that returns true if both a and b are saying hello or if neither of them are. Print false otherwise.</td>&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr&nbsp; class="easy">&nbsp; &nbsp; &nbsp; <td>Functions</td>&nbsp; &nbsp; &nbsp; <td>Easy</td>&nbsp; &nbsp; &nbsp; <td>sum</td>&nbsp; &nbsp; &nbsp; <td>Write a function named sum that takes arguments x,y and returns the sum of two numbers.</td>&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr class="medium">&nbsp; &nbsp; &nbsp; <td>Loops</td>&nbsp; &nbsp; &nbsp; <td>Medium</td>&nbsp; &nbsp; &nbsp; <td>count</td>&nbsp; &nbsp; &nbsp; <td>Write a function named count that takes two arguments sequence and item that returns the number of times the item occurs in the list.</td>&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr class="easy">&nbsp; &nbsp; &nbsp; <td>Functions</td>&nbsp; &nbsp; &nbsp; <td>Easy</td>&nbsp; &nbsp; &nbsp; <td>mult</td>&nbsp; &nbsp; &nbsp; <td>Write a function named mult that takes parameters a,b,c and returns the product of those three numbers.</td>&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr class="hard">&nbsp; &nbsp; &nbsp; <td>Strings</td>&nbsp; &nbsp; &nbsp; <td>Hard</td>&nbsp; &nbsp; &nbsp; <td>appendMiddle</td>&nbsp; &nbsp; &nbsp; <td>Write a function named appendMiddle that takes arguments s1 and s2 and creates a new string by appending s2 in the middle of s1.</td>&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr class="hard">&nbsp; &nbsp; &nbsp; <td>Strings</td>&nbsp; &nbsp; &nbsp; <td>Hard</td>&nbsp; &nbsp; &nbsp; <td>findAll</td>&nbsp; &nbsp; &nbsp; <td>Write a function named findAll that takes an input string as its argument and counts all lowercase,uppercase,digits,and special symbols.Then prints the counts.</td>&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr class="hard">&nbsp; &nbsp; &nbsp; <td>Loops</td>&nbsp; &nbsp; &nbsp; <td>Hard</td>&nbsp; &nbsp; &nbsp; <td>digit_sum</td>&nbsp; &nbsp; &nbsp; <td>Write a function called digit_sum that takes a positive integer n as input and returns the sum of all that number's digits.</td>&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr class="hard">&nbsp; &nbsp; &nbsp; <td>Loops</td>&nbsp; &nbsp; &nbsp; <td>Hard</td>&nbsp; &nbsp; &nbsp; <td>factorial</td>&nbsp; &nbsp; &nbsp; <td>Write a function named factorial that takes a non-negative integer x that can multiply all the integers from 1 through x.</td>&nbsp; &nbsp; </tr></table>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5