如何在javascript中使用开关条件?
我想在JavaScript代码中使用开关条件。但是我不知道怎么做?请帮我做这件事。。提前谢谢如何在javascript中使用开关条件?,javascript,asp.net,switch-statement,Javascript,Asp.net,Switch Statement,我想在JavaScript代码中使用开关条件。但是我不知道怎么做?请帮我做这件事。。提前谢谢 switch(n) { case 1: execute code block 1 break; case 2: execute code block 2 break; default: code to be executed if n is different from case 1 and 2 } 你的问题没有太多细节,但无论如何我都会尽力帮助你——但在不了解全部情况的情况下,给
switch(n)
{
case 1:
execute code block 1
break;
case 2:
execute code block 2
break;
default:
code to be executed if n is different from case 1 and 2
}
你的问题没有太多细节,但无论如何我都会尽力帮助你——但在不了解全部情况的情况下,给你一个明确的答案有点困难 我假设您希望在单击按钮时处理重定向,但有一些条件吗 通常,您会使用一个简单的case语句来处理此问题(如果只有两个条件,则使用if/else),例如以下内容(使用jQuery进行事件绑定):
按钮1
按钮2
jQuery(函数(){
jQuery(“按钮1,按钮2”)。单击(函数(e){
开关(jQuery(this.attr(“id”)){
案例“button1”:
location.href=”http://clickedonbutton1.com";
打破
案例“按钮2”:
location.href=”http://clickedonbutton2.com";
打破
违约:
警惕(“不知道这里发生了什么……”);
}
});
});
读取
大小写值
类型没有限制在问题中添加你的代码以获得更好的答案。这不一样。C++中的开关语句中不能使用字符串。
switch(youVariable)
{
case "test1":
break;
case "test2":
break;
default:
break;
}
<button id="button1">Button 1</button>
<button id="button2">Button 2</button>
<script type="text/javascript">
jQuery(function() {
jQuery("button1, button2").click(function(e) {
switch(jQuery(this).attr("id")) {
case "button1":
location.href = "http://clickedonbutton1.com";
break;
case "button2":
location.href = "http://clickedonbutton2.com";
break;
default:
alert("Dont know what happened here....");
}
});
});
</script>