我想创建4个级联下拉列表

时间:2015-03-05 13:05:18

标签: javascript jquery ajax html5 jquery-plugins

我想创建4个级联下拉列表JavaScript我来到这个代码在线,但我希望我想在此代码上添加最后一个输入,最后一个输入取决于citySelect。请帮助我一直在搜索,我只找到三个级联下拉列表

            <form name="myform" id="myForm">
                <select name="optone" id="stateSel" size="1">
                    <option value="" selected="selected">Select state</option>
                </select>
                <br>
                <br>
                <select name="opttwo" id="countySel" size="1">
                    <option value="" selected="selected">Please select state first</option>
                </select>
                <br>
                <br>
                <select name="optthree" id="citySel" size="1">
                    <option value="" selected="selected">Please select county first</option>
                </select>
                <br>
                <br>
                <select name="optfour" id="branchSel" size="1">
                    <option value="" selected="selected">Please select branch first</option>
                </select>
            </form>

            <script>

            var stateObject = {
                "California": {
                    "Monterey": ["Salinas", "Gonzales"],
                    "Alameda": ["Oakland", "Berkeley"]
                },
                "Oregon": {
                    "Douglas": ["Roseburg", "Winston"],
                    "Jackson": ["Medford", "Jacksonville"]
                }
            }
            window.onload = function () {
                var stateSel = document.getElementById("stateSel"),
                    countySel = document.getElementById("countySel"),
                    citySel = document.getElementById("citySel");
                for (var state in stateObject) {
                    stateSel.options[stateSel.options.length] = new Option(state, state);
                }
                stateSel.onchange = function () {
                    countySel.length = 1; // remove all options bar first
                    citySel.length = 1; // remove all options bar first
                    if (this.selectedIndex < 1) return; // done   
                    for (var county in stateObject[this.value]) {
                        countySel.options[countySel.options.length] = new Option(county, county);
                    }
                }
                stateSel.onchange(); // reset in case page is reloaded
                countySel.onchange = function () {
                    citySel.length = 1; // remove all options bar first
                    if (this.selectedIndex < 1) return; // done   
                    var cities = stateObject[stateSel.value][this.value];
                    for (var i = 0; i < cities.length; i++) {
                        citySel.options[citySel.options.length] = new Option(cities[i], cities[i]);
                    }
                }
            }

            </script>

1 个答案:

答案 0 :(得分:0)

我会选择开箱即用的解决方案。更快更容易实施。

例如:http://plugins.krajee.com/dependent-dropdown/demo

希望这有帮助!