jquery - 如何为 jQuery 制作 switch case?

标签 jquery if-statement switch-statement

我有这个 if-else 语句,它给了我奇怪的响应...每当我首先选择“输出”时,之后选择的任何其他内容都不会出现... 仅供引用,我正在使用多项选择,因此我可以选择并显示任意数量的内容。

 $('#outputText').hide();
    $('#armCB').hide();
    $('#outputCB').hide();
    $('#zoneText').hide();
    $('#counterText').hide();
    $('#flagText').hide();
    $('#sensorText').hide();

('#select-choice-1').change(function(){
        if ($('#output').is(':selected')){
            $('#outputText').show();
        }
        else if ($('#arm').is(':selected')){
            $('#armCB').show();
        }
        else if ($('#zone').is(':selected')){
            $('#zoneText').show();
        }
        else if ($('#counter').is(':selected')){
            $('#counterText').show();
        }
        else if ($('#flag').is(':selected')){
            $('#flagText').show();
        }
        else if ($('#sensor').is(':selected')){
            $('#sensorText').show();
        }
        else{
            $('#outputText').hide();
            $('#armCB').hide();
            $('#zoneText').hide();
            $('#counterText').hide();
            $('#flagText').hide();
            $('#sensorText').hide();

        }

我的 if-else 语句有错误吗?或者我必须在这里使用 Switch case 语句?如果是的话我该怎么办?

HTML:

<div id="display" style=" clear:both">
        <div class="left" style="float:left; width:48%">
        <div data-role="fieldcontain">
            <label for="select-choice-1" class="select">Select Category</label>
            <select name="select-choice-1" id="select-choice-1" data-native-menu="false" data-mini="true" multiple="multiple" size="2">

                <option value="arm" id="arm">Arm</option>
                <option value="zone" id="zone">Zone Input</option>
                <option value="output" id="output">Output</option>
                <option value="counter" id="counter">Counter</option>
                <option value="flag" id="flag">Flag</option>
                <option value="sensor" id="sensor">Sensor</option>
            </select>
        </div>



        </div>
        <div class="right" style=" float: right; width:48%">
             <div id="armCB">
                <fieldset data-role="controlgroup">
                    <legend>Unit</legend>
                    <input type="checkbox" class="CB" name="armCB_1" id="armCB_1" class="custom" data-mini="true" />
                    <label for="armCB_1">Arming Status</label>
                </fieldset>
             </div>

            <div id="outputText">
                <p> Please enter an Output number and press "Add" button: </p>
                <input style="background: white; color: black;" type="text" id="outputTextInput" value="">
                <input type="submit" id="outputAdd" value="Add"/>
                <input type="submit" id="outputRemove" value="Remove"/>
            </div>
            <div id="zoneText">
                <p> Please enter a Zone number and press "Add" button: </p>
                <input style="background: white; color: black;" type="text" id="zoneTextInput" value="">
                <input type="submit" id="zoneAdd" value="Add"/>
                <input type="submit" id="zoneRemove" value="Remove"/>
            </div>
            <div id="counterText">
                <p> Please enter a counter number and press "Add" button: </p>
                <input style="background: white; color: black;" type="text" id="counterTextInput" value="">
                <input type="submit" id="counterAdd" value="Add"/>
                <input type="submit" id="counterRemove" value="Remove"/>
            </div>
            <div id="flagText">
                <p> Please enter a Flag number and press "Add" button: </p>
                <input style="background: white; color: black;" type="text" id="flagTextInput" value="">
                <input type="submit" id="flagAdd" value="Add"/>
                <input type="submit" id="flagRemove" value="Remove"/>
            </div>
            <div id="sensorText">
                <p> Please enter a Sensor number and press "Add" button: </p>
                <input style="background: white; color: black;" type="text" id="sensorTextInput" value="">
                <input type="submit" id="sensorAdd" value="Add"/>
                <input type="submit" id="sensorRemove" value="Remove"/>
            </div>
        </div>
    </div>

最佳答案

这只是标准的 javascript : 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
}                           

其中开关变量将是所选标签的名称

关于jquery - 如何为 jQuery 制作 switch case?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16139948/

相关文章:

javascript - 使用 select2 在悬停/鼠标悬停时激活选择菜单

javascript - 如何使用 jQuery 将正文类添加到首页和内部页面?

从 stdin 计算简单赋值运算符 "="

c - 编码文本和文件写入读取错误

c# - 一个 'else' 用于嵌套的 'if' 语句

Jquery 选项卡在单击按钮时重新加载内容

html - 使用 CSS 使开关切换更暗

javascript - switch 语句中的变量 case 列表

java - Java 中的数字到单词的转换

Javascript/Jquery - 正则表达式中的变量