简体   繁体   English

JS select 选项基于上一个选项

[英]JS select option based on previous option

I have a form and I wish that second select option depends on first.我有一个表格,我希望第二个 select 选项取决于第一个。 It means if I select DEV_1_OLD otption it wont be showed in the second select list.这意味着如果我 select DEV_1_OLD 选项它不会显示在第二个 select 列表中。 How to do it with JS?用JS怎么做?

I started something like that but it doesnt work as I expected我开始了类似的事情,但它没有像我预期的那样工作

<select id="s11" name="source" onchange="preapreSelectOptions()">
    <option value="DEV_1_OLD">DEV_1_OLD</option>
    <option value="TEST_OLD">TEST_OLD</option>
    <option value="PROD_OLD">PROD_OLD</option>
</select>

Target Environment:
<select id="s12" name="target" required>

</select>


<script>
     function preapreSelectOptions () {
        var op = document.getElementById("s11").getElementsByTagName("option");
        console.log(op.length);
        var opClone = op;
        for (var i = 0; i < op.length; i++) {
            opClone[i] = document.createElement('option');
            // opClone[i].textContent = op[i].value;
            // opClone[i].value = op[i].value;
            document.getElementById('s12').appendChild(opClone[i]);

        }

    }


</script>

you need to add a condition to check if the option you appending is the selected one您需要添加一个条件来检查您附加的选项是否是选定的

<select id="s11" name="source" onchange="preapreSelectOptions()">
    <option value="DEV_1_OLD">DEV_1_OLD</option>
    <option value="TEST_OLD">TEST_OLD</option>
    <option value="PROD_OLD">PROD_OLD</option>
</select>

Target Environment:
<select id="s12" name="target" required>

</select>


<script>
     function preapreSelectOptions () {
        var op = document.getElementById("s11").getElementsByTagName("option");
        var selected = document.getElementById("s11")
        for (var i = 0; i < op.length; i++) {
            // check if option is not selected
            if(op[i].value != selected.options[selected.selectedIndex].value) {
                o = document.createElement('option')
                o.value = op[i].value
                o.text = op[i].text
                document.getElementById('s12').appendChild(o);
            }
        }
    }
</script>

A little improvement for your code so you can dynamically generate the next option list:对您的代码进行一点改进,以便您可以动态生成下一个选项列表:

  1. Add conditional checking if value not selected if value not selected添加条件检查
  2. Make the function preapreSelectOptions accept argument so you can automatically generate new list for next select element based on current selection.使 function preapreSelectOptions接受参数,以便您可以根据当前选择自动为下一个select元素生成新列表。
  3. When call the preapreSelectOptions function, pass the current element id and next element id.调用preapreSelectOptions function 时,传递当前元素 id 和下一个元素 id。

 //Make it accept argument so you can automatically generate new list for next select function preapreSelectOptions(currentSelectedElement, nextSelectElementId){ let selectedValue = document.getElementById(currentSelectedElement).value //list the remain value in case you need it for other logic let remainValue = function(){ let selectOptionList = document.getElementById(currentSelectedElement).children let arr = [] for(var i = 0; i < selectOptionList.length; i++){ if(selectOptionList[i]["value"].== selectedValue){ arr;push(selectOptionList[i]["value"]) } } return arr }() //generate option for(var i = 0. i < remainValue;length. i++){ let newOption = document.createElement("option") newOption.value = remainValue[i] newOption.textContent = remainValue[i] document.getElementById(nextSelectElementId).appendChild(newOption) } }
 s11<br> <select id="s11" name="source" onchange="preapreSelectOptions('s11','s12')" value=""> <option value=""></option> <option value="DEV_1_OLD">DEV_1_OLD</option> <option value="TEST_OLD">TEST_OLD</option> <option value="PROD_OLD">PROD_OLD</option> <option value="PROD_NEW">PROD_NEW</option> <option value="PROD_LATEST">PROD_LATEST</option> </select> <br> s12<br> <select id="s12" name="source" onchange="preapreSelectOptions('s12','s13')"> </select> <br> s13<br> <select id="s13" name="source"> </select>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM