[英]Why is my switch statement running multiple cases?
I have the following function: 我有以下功能:
$("#drpType").change(function () {
var SelectedVal = this.value;
switch (SelectedVal) {
case "2021":
console.log('a');
console.log('b');
case "2020":
console.log('a');
case "ADB":
console.log('b');
case "PWP":
console.log('c');
}
}
Why, when SelectedVal
is 2021, is it printing a,b,a,b,c
? 为什么在SelectedVal
为2021时打印a,b,a,b,c
?
Created scenario in jsfiddle . 在jsfiddle中创建了场景。 Now i am able to understand you my question. 现在我可以理解你的问题了。 When select 2021
it return 2 divs. 选择2021
时返回2格。 and if switch statement base on fall-thought so why its happning that if select EFGH
the only one div visible instead of 3? 并且如果switch语句基于秋天的思想,那么为什么它发生,如果选择EFGH
则只有一个div可见而不是3?
<select id="drpQuotaType" >
<option value="0">Select</option>
<option value="2021">2021</option>
<option value="2020">2020</option>
<option value="ABCD">ABCD</option>
<option value="EFGH">EFGH</option>
</select>
<div id="dv1" style="display:none">Div 1 </div></br>
<div id="dv2" style="display:none">Div 2 </div></br>
<div id="dv3" style="display:none">Div 3 </div></br>
<div id="dv4" style="display:none">Div 4 </div>
$("#drpQuotaType").change(function () {
var SelectedVal = this.value;
$('#dv1').hide();
$('#dv2').hide();
$('#dv3').hide();
switch (SelectedVal) {
case "2021":
$('#dv1').show();
$('#dv2').show();
case "2020":
$('#dv2').show();
case "ABCD":
$('#dv2').show();
case "EFGH":
$('#dv3').show();
}
});
You are missing your break;
你错过了break;
statements on each of your cases: 关于每种情况的陈述:
$("#drpType").change(function () {
var SelectedVal = this.value;
switch (SelectedVal) {
case "2021":
console.log('a');
console.log('b');
break;
case "2020":
console.log('a');
break;
case "ADB":
console.log('b');
break;
case "PWP":
console.log('c');
break;
}
It's because you don't have a break
statement. 这是因为您没有break
语句。 Without it, the code will "fall through". 没有它,代码将“失败”。
var x = 10; switch (x) { case 10: console.log('With break'); break; case 20: console.log('I never run'); break; } console.log('-------'); switch (x) { case 10: console.log('Without'); case 20: console.log('a break'); case 30: console.log('it just'); case 40: console.log('keeps going'); }
The reason this exists is because sometimes it's useful to fall through. 之所以存在,是因为有时跌倒很有用。 You could imagine a case where you're updating the state of a game and what stuff to happen for a specific kind of enemy as well as for all enemies. 您可以想象这样一种情况:您要更新游戏的状态以及特定敌人以及所有敌人会发生什么事情。
var characterType = 'big boss'; switch (characterType) { case 'big boss': console.log('Update the big boss'); case 'enemy': console.log('The big boss is also an enemy so run the basic enemy code'); break; case 'player': console.log('The boss is not a player so we will not run this.'); break; }
It's not incredibly common to utilize fall-through but it does have its use cases. 利用穿插并不罕见,但确实有其用例。
Finally, here's what your code should look like: 最后,代码如下所示:
$("#drpType").change(function () {
var SelectedVal = this.value;
switch (SelectedVal) {
case "2021":
console.log('a');
console.log('b');
break; // <--
case "2020":
console.log('a');
break; // <--
case "ADB":
console.log('b');
break; // <--
case "PWP":
console.log('c');
break; // optional. It's visually consistent but doesn't do anything
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.