如何获取所选选项的数量

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何获取所选选项的数量相关的知识,希望对你有一定的参考价值。

我需要一些帮助。我正在尝试使用“no”类获取我的元素中所选选项的长度(意味着使用无类名称获取所选选项的数量。复制并粘贴到浏览器中以查看我正在谈论的内容并选择选择你自己看)。

这是我的html

function Starting(e) 
  e.preventDefault();
  var artchoices = document.querySelectorAll(".art");
  var program = document.querySelectorAll(".program");
  var choice = document.querySelectorAll(".choice");
  var no = document.querySelectorAll(".no");
  var music = document.querySelectorAll(".music");
  var allchoices;

  for (var e = 0; e < choice.length; e++) 
    if (choice[e].selected == true) 
      allchoices = true;
      break;
    
  

  if (allchoices)
    console.log("Please make sure all options are selected");
  else 
    if (no.length.selected >= choice.length)
      console.log("hello");
  
  return false;
<!DOCTYPE HTML>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="quiz" content="myown quiz">
  <title>Quiz</title>
</head>

<body>
  <script type="text/javascript" src="machine.js"></script>
  <link rel="stylesheet" type="text/css" href="quizformatting.css">
  <h1>Choose the major right for you</h1>
  <pre>
    <form>
    Do you like enjoy programming?
    <select>
    <option class="choice">Select a value</option>
    <option class="program">Yes</option>
    <option class="no">No</option>
    </select>
    
    
    Do you enjoy 2d animation and 3d animation?
    <select>
    <option class="choice">Select a value</option>
    <option class="art">Yes</option>
    <option class="no">no</option>
    </select>
    
    
    Do you like music
    <select>
    <option class="choice">Select a value</option>
    <option>Yes</option>
    <option class="no">no</option>
    </select>
    
    
    
    What are your favorite pastimes?
    <select>
    <option class="choice">Select a value</option>
    <option class="music">Listening to music</option>
    <option>making websites</option>
    <option class="art">Drawing</option>
    <option class="no">None of these</option>
    </select>
    
    
    
    
    Out of all the activities you like to do, which one do you enjoy the most?
    <select>
    <option class="choice">Select a value</option>
    <option class="art">Painting and drawing</option>
    <option class="music">Playing instruments</option>
    <option class="art">Drawing</option>
    <option class="no">None of these</option>
    </select>
    
    
    
    Would you be interested in making art or coding for video games?
    <select>
    <option class="choice">Select a value</option>
    <option class="program">I would be interested in learning the programming 
    languages used to create the scripting for games</option>
    <option class="art">I would like to the models and the environment for 
     modeling</option>
    <option class="no">I'm not interested in either of these options</option>
    </select>
    
    
    
    Do you enjoy making websites or learning how to sing?
    <select>
    <option class="choice">Select a value</option>
    <option class="music">Learning how to sing</option>
    <option class="program">making websites for projects</option>
    <option class="no">I'm not interested in any of this</option>
    </select>
    
    
    
    
    Do you enjoy listening to music more or making programming applications?
    <select>
    <option class="choice">Select a value</option>
    <option class="music">I would like to listen to music</option>
    <option class="program">Programming is my thing</option>
    <option class="art">I'm more of a drawer</option>
    <option class="no">I don't like any of these options</option>
    </select>
    
    
    
    Which skillset are you more interested in learning?
    <select>
    <option class="choice">Select a value</option>
    <option class="music">Learning the notes of instruments</option>
    <option class="program">Learning the language of javascript</option>
    <option class="art">I like anime, so I would love to learn how to animate in 
    anime style</option>
    <option class="no">I don't want to do any of these options</option>
    </select>
    
    
    
    Please press the button to get your answer
    
    <button onclick="Starting(event);">Click me</button>
    </form>
    </pre>
</body>

</html>




Here is my javascript file:
答案

如果您有权访问jQuery,则可以使用以下脚本:

$('form option.no:selected').length

另一答案

您可以使用:checked伪类来查找所选的所有选项元素。在这种情况下,我已经更改了你在querySelectorAll中使用的选择器,这样no只有选择的类别为“no”的选项。

正如@PatrickRoberts在评论中提到的那样,使用value属性是一个更好的主意。首先,它在语义上是正确的(options应该有一个value表单提交)。

function Starting(e) 
  e.preventDefault();
  var artchoices = document.querySelectorAll(".art");
  var program = document.querySelectorAll(".program");
  var choice = document.querySelectorAll(".choice");
  var no = document.querySelectorAll(".no:checked");
  var music = document.querySelectorAll(".music");
  var allchoices;

  for (var e = 0; e < choice.length; e++) 
    if (choice[e].selected == true) 
      allchoices = true;
      break;
    
  

  if (allchoices)
    console.log("Please make sure all options are selected");
  else 
    if (no.length >= choice.length)
      console.log("hello");
  
  return false;
<!DOCTYPE HTML>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="quiz" content="myown quiz">
  <title>Quiz</title>
</head>

<body>
  <script type="text/javascript" src="machine.js"></script>
  <link rel="stylesheet" type="text/css" href="quizformatting.css">
  <h1>Choose the major right for you</h1>
  <pre>
    <form>
    Do you like enjoy programming?
    <select>
    <option class="choice">Select a value</option>
    <option class="program">Yes</option>
    <option class="no">No</option>
    </select>
    
    
    Do you enjoy 2d animation and 3d animation?
    <select>
    <option class="choice">Select a value</option>
    <option class="art">Yes</option>
    <option class="no">no</option>
    </select>
    
    
    Do you like music
    <select>
    <option class="choice">Select a value</option>
    <option>Yes</option>
    <option class="no">no</option>
    </select>
    
    
    
    What are your favorite pastimes?
    <select>
    <option class="choice">Select a value</option>
    <option class="music">Listening to music</option>
    <option>making websites</option>
    <option class="art">Drawing</option>
    <option class="no">None of these</option>
    </select>
    
    
    
    
    Out of all the activities you like to do, which one do you enjoy the most?
    <select>
    <option class="choice">Select a value</option>
    <option class="art">Painting and drawing</option>
    <option class="music">Playing instruments</option>
    <option class="art">Drawing</option>
    <option class="no">None of these</option>
    </select>
    
    
    
    Would you be interested in making art or coding for video games?
    <select>
    <option class="choice">Select a value</option>
    <option class="program">I would be interested in learning the programming 
    languages used to create the scripting for games</option>
    <option class="art">I would like to the models and the environment for 
     modeling</option>
    <option class="no">I'm not interested in either of these options</option>
    </select>
    
    
    
    Do you enjoy making websites or learning how to sing?
    <select>
    <option class="choice">Select a value</option>
    <option class="music">Learning how to sing</option>
    <option class="program">making websites for projects</option>
    <option class="no">I'm not interested in any of this</option>
    </select>
    
    
    
    
    Do you enjoy listening to music more or making programming applications?
    <select>
    <option class="choice">Select a value</option>
    <option class="music">I would like to listen to music</option>
    <option class="program">Programming is my thing</option>
    <option class="art">I'm more of a drawer</option>
    <option class="no">I don't like any of these options</option>
    </select>
    
    
    
    Which skillset are you more interested in learning?
    <select>
    <option class="choice">Select a value</option>
    <option class="music">Learning the notes of instruments</option>
    <option class="program">Learning the language of javascript</option>
    <option class="art">I like anime, so I would love to learn how to animate in 
    anime style</option>
    <option class="no">I don't want to do any of these options</option>
    </select>
    
    
    
    Please press the button to get your answer
    
    <button onclick="Starting(event);">Click me</button>
    </form>
    </pre>
</body>

</html>




Here is my javascript file:

以上是关于如何获取所选选项的数量的主要内容,如果未能解决你的问题,请参考以下文章

如何获取所选选项的文本而不是值

Qt tableWidget 如何获取当前所选tableWidgetItem的数量 和内容

jQuery Chosen:如何从多项选择中获取所选选项文本的数组

在我的情况下,如何在 <select> 中获取所选选项的 ID?

使用 jQuery 获取所选选项的索引

使用 jQuery 获取所选选项的文本 [重复]