如何在javascript中使用切换条件?

发布于 2024-12-05 12:21:48 字数 62 浏览 5 评论 0原文

我想在 JavaScript 代码中使用 switch 条件。但我不知道怎么办?请帮助我做到这一点..提前致谢

I want to use switch condition in JavaScript code. but i dont know how? please help me to do this.. thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

终遇你 2024-12-12 12:21:48
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
}

http://www.w3schools.com/js/js_switch.asp

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
}

http://www.w3schools.com/js/js_switch.asp

沉默的熊 2024-12-12 12:21:48

你的问题没有太多细节,但无论如何我都会尽力提供帮助 - 但在不了解完整情况的情况下,很难给你一个明确的答案。

我假设您想要处理单击按钮时的重定向,但有一些条件吗?

通常您会使用一个简单的 case 语句来处理这个问题(如果只有两个条件,则使用 if/else ),如下所示(使用 jQuery 进行事件绑定):

<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>

You don't have a lot of detail in your question but I'll try to help regardless - but without knowing the full scenario its a bit hard to give you a definitive answer.

I'm going to assume you want to handle a redirection on a button click, but with some conditions?

Usually you would use a simple case statement to handle this (or an if/else if there is only two conditions), such as the following (using jQuery for event bindings):

<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>
伪心 2024-12-12 12:21:48

阅读switch

  1. 大小写值没有限制 类型。

  2. 您可以在 JavaScript 中使用“字符串”大小写值

  3. 您可以添加重复的大小写值。无论是否重复,都会识别第一个匹配的大小写值。

Read switch

  1. No restriction on case value type.

  2. You can use "string" case value in JavaScript

  3. You can add duplicate case value. The first matching case value is recognized, regardless of duplicates.

后知后觉 2024-12-12 12:21:48
switch(youVariable)
{
   case "test1":
   break;

   case "test2":
   break;

   default:
   break;

}
switch(youVariable)
{
   case "test1":
   break;

   case "test2":
   break;

   default:
   break;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文