简体   繁体   English

如何使具有多个类的下拉菜单显示/隐藏div?

[英]How can I make a dropdown menu show/hide divs that have several classes?

I'm brand-new to coding, I've done days of research on this problem, and I've reached a dead-end. 我是编码的新手,我已经对这个问题进行了数天的研究,但是我已经走到了尽头。

I have a simple webpage with a series of divs that contain tutor profiles. 我有一个简单的网页,其中包含一系列包含教师资料的div。 Each div has multiple classes with all of the subjects that each tutor can tutor. 每个div都有多个班级,每个班级的每个科目都可以辅导。 There is overlap. 有重叠。 For example, many tutors can tutor Algebra 1, so when someone selects "Algebra 1" in the dropdown, I want only the tutors with "algebra-one" in their div class to show up. 例如,许多辅导员可以辅导代数1,因此当有人在下拉列表中选择“代数1”时,我只希望在其div类中显示“代数为1”的辅导员。 When someone selects the "all items" option, I want all the profiles to show again (that's working fine). 当有人选择“所有项目”选项时,我希望所有配置文件再次显示(工作正常)。 When the page loads, I want all the profiles to show (working fine, too). 页面加载时,我希望所有配置文件都显示(也可以正常工作)。

The problem is that the dropdown stops working past the 4th option: Algebra 2. Why? 问题在于下拉菜单在第四个选项之后停止工作:代数2。为什么? How do I fix this? 我该如何解决? Is there an easier way to do what I'm trying to do? 有没有更简单的方法可以做我想做的事情?

THE CODE (I found some code on another Stackoverflow question and modified it up to this point): https://jsfiddle.net/hyptastic/d90kdmgg/ 代码 (我在另一个Stackoverflow问题上找到了一些代码,并对此进行了修改): https : //jsfiddle.net/hyptastic/d90kdmgg/

<select id="classes" multiple="multiple">
<option value="classes">all items</option>
<option value="sixth-grade-math">6th Grade Math</option>
<option value="algebra-one">Algebra 1</option>
<option value="algebra-two">Algebra 2</option>
<option value=“biology”>Biology</option>
<option value=“calculus”>Calculus</option>
<option value=“chemistry”>Chemistry</option>
<option value=“english”>English</option>
<option value=“geometry”>Geometry</option>
<option value=“government-economics”>Government/Economics</option>
<option value=“history”>History</option>
<option value=“homework-help”>Homework Help</option>
<option value=“physical-science”>Physical Science</option>
<option value=“physics”>Physics</option>
<option value=“pre-algebra”>Pre-Algebra</option>
<option value=“pre-calculus”>Pre-Calculus</option>
<option value=“spanish”>Spanish</option>
<option value=“statistics”>Statistics</option>
<option value=“test-prep”>Test Prep</option>
</select>

<div class="classes sixth-grade-math">Tutor 1</div>

<div class="classes sixth-grade-math algebra-one">Tutor 2</div>

<div class="classes sixth-grade-math algebra-one algebra-two">Tutor 3</div>

<div class="classes sixth-grade-math algebra-one algebra-two biology">Tutor 4</div>

<div class="classes biology">Tutor 5</div>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Javascript: Javascript:

$('#classes').on('change', function () {
var number = $(this).val();
var classNames = '.' + number.join('.');
$(".classes").hide().filter(classNames).show();
});

In my browser of chrome 53 and edge, it run error in case that option value is biology . 在我的Chrome 53和Edge浏览器中,如果选项值为biology ,它将运行错误。

I thought you typed wrong Chinese quotes symbol “” , you'd better check its symbols' encoding at before. 我以为您输入了错误的中文引号“” ,因此最好在之前检查其符号的编码。

You messed up your quotes for the lower values,moral is dont copy paste. 您将较低的报价弄乱了,道德是不要复制粘贴。

“calculus” is not the same as "calculus" “calculus”"calculus"

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

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