简体   繁体   English

隐藏/显示辅助选择中的所选选项

[英]Hide/show the selected option on a secondary select

I have two <select> elements with different IDs. 我有两个具有不同ID的<select>元素。

When the user selects a value from the first select box, I want the second select box to only display connected values. 当用户从第一个选择框中选择一个值时,我希望第二个选择框仅显示连接的值。

My code: 我的代码:

<select id="ExtraField_1" name="ExtraField_1">
    <option value="1">test1</option>
    <option value="2">test2</option>
    <option value="3">test3</option>
    <option value="4">test4</option>
    <option value="5">test5</option>
    <option value="6">test6</option>
    <option value="7">test7</option>
    <option value="8">test8</option>
    <option value="9">test9</option>
    <option value="10">test10</option>
    <option value="11">test11</option>
    <option value="12">test12</option>
</select>
<select id="ExtraField_2" name="ExtraField_2">
    <option value="1">test1</option>
    <option value="2">test2</option>
    <option value="3">test3</option>
    <option value="4">test4</option>
    <option value="5">test5</option>
    <option value="6">test6</option>
    <option value="7">test7</option>
    <option value="8">test8</option>
    <option value="9">test9</option>
    <option value="10">test10</option>
    <option value="11">test11</option>
    <option value="12">test12</option>
    <option value="13">test13</option>
    <option value="14">test14</option>
    <option value="15">test15</option>
    <option value="16">test16</option>
    <option value="17">test17</option>
    <option value="18">test18</option>
    <option value="19">test19</option>
    <option value="20">test20</option>
</select>

So when user selects "test1" from first select boxm he will see only "test2", "test3" and "test4" on the second select box; 因此,当用户从第一个选择框选择“ test1”时,他在第二个选择框上只会看到“ test2”,“ test3”和“ test4”; "test2" from first will show "test6", "test7" and "test8" in the second box. 第一个中的“ test2”将在第二个框中显示“ test6”,“ test7”和“ test8”。

How can I use JavaScript to resolve this problem? 如何使用JavaScript解决此问题?

If you can use jQuery then you can always just clear and append the options to the second select. 如果可以使用jQuery,则始终可以清除并附加选项到第二个选择中。

    $('#ExtraField_1').change(function(){
        $('#ExtraField_2').find('option').remove()
        if($(this).val() == '1'){
           $('#ExtraField_2').append($("<option </option>").attr('value','2').text('test2'));
           $('#ExtraField_2').append($("<option></option>").attr('value','3').text('test3'));
           $('#ExtraField_2').append($("<option></option>").attr('value','4').text('test4'));
        }
        if($(this).val() == '2'){
           $('#ExtraField_2').append($("<option></option>").attr('value','5').text('test5'));
           $('#ExtraField_2').append($("<option></option>").attr('value','6').text('test6'));
           $('#ExtraField_2').append($("<option></option>").attr('value','7').text('test7'));
        } 
   });

http://jsfiddle.net/8XVuv/2/ http://jsfiddle.net/8XVuv/2/

using only javascript is a bit more complicated but I would still take the same approach. 仅使用javascript有点复杂,但是我仍然会采用相同的方法。

    function createOption(otext,oValue){
        var newOption = document.createElement('option');
        newOption.text = otext;
        newOption.value = oValue;
        return newOption;
    }

    function clearSelect(theSelect){
        for(var i = 0;i <= theSelect.options.length+1;i++)
        {
            theSelect.remove();
        }
    }

    function onSelect(theSelect){
        var nextSelect = document.getElementById('ExtraField_2');
        clearSelect(nextSelect);
        var selected = theSelect.options[theSelect.selectedIndex];

        if(selected.value == 1){
            nextSelect.add(createOption('test2','2'));
            nextSelect.add(createOption('test3','3'));
            nextSelect.add(createOption('test4','4'));
        }
        if(selected.value == 2){
            nextSelect.add(createOption('test5','5'));
            nextSelect.add(createOption('test6','6'));
            nextSelect.add(createOption('test7','7'));
        }

    }

with html: 与html:

<select id="ExtraField_1" name="ExtraField_1" onchange="javascript: onSelect(this);" >
    <option value="0">Select a test..</option>
    <option value="1">test1</option>
    <option value="2">test2</option>
    <option value="3">test3</option>
    <option value="4">test4</option>
    <option value="5">test5</option>
    <option value="6">test6</option>
    <option value="7">test7</option>
    <option value="8">test8</option>
    <option value="9">test9</option>
    <option value="10">test10</option>
    <option value="11">test11</option>
    <option value="12">test12</option>
</select>

<select id="ExtraField_2" name="ExtraField_2">
    <option value="0">Select from the left</option>
</select>​

as you can see it still does what you expect but you are not hiding options. 如您所见,它仍然可以满足您的期望,但您没有隐藏任何选项。

http://jsfiddle.net/upKzW/13/ http://jsfiddle.net/upKzW/13/

 $('#ExtraField_1').change(function() {
 $('#ExtraField_2').val(this.value);

}); });

http://jsfiddle.net/judearasu/rF8G6/ http://jsfiddle.net/judearasu/rF8G6/

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

相关问题 选择:在第二选择上隐藏选择的选项 - SELECTS: Hide the selected option on a secondary select 显示/隐藏所选单选按钮的选项 - Show/Hide Select Option for selected radio buttons 当在另一个选择中选择一个选项时,是否可以显示/隐藏某个选项并显示/隐藏其他选项? - Is there a way to show/hide some option and show/hide others in a select when an option is selected in a different select? 如何在选择 select 选项时显示 div 并在取消选择时隐藏? - How to show a div when a select option is selected and hide when unselected? 下面的“隐藏/显示分区”取决于是否选择了一个选项 - Hide/Show Div below select depending on whether an option is selected or not 显示隐藏所选选项触发第二个选择选项然后第二个选择选项触发隐藏显示div - show hide the selected option trigger 2nd select option then 2nd select option trigger hide show div 隐藏/显示<option value="in select element when another option in another select is selected">在select元素中选择另一个选项,选择了另一个选项</option> - Hide/Show <option> in select element when another option in another select is selected 根据选择选项隐藏和显示选择选项 - Hide and show select option based on select option 如果选择了选择选项,则显示字段 - Show fields IF select option is selected 选择框选项未显示为已选中 - Select box option not show selected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM