Javascript链下拉列表选择 [英] Javascript Chain Dropdown Select

查看:40
本文介绍了Javascript链下拉列表选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道网上有很多这样的问题,但是我仍然很难找到我需要的东西.

I know that there are a lot of this questions being posted online but I still have difficulty finding what I need.

目前,我发现某项正在最多进行3个链接的下拉选择.我需要有4个,并且我曾尝试编辑脚本,但无法正常工作.

Currently I have found something that is doing 3 chained dropdown selection maximum. I needed to have 4 and I have tried editing the script but it's not working.

下面是我编辑的代码,我的代码有什么问题吗?

Below is the code that I have edited, is there anything wrong with my code?

    <html>
<head>

<script type="text/javascript">


// State lists
var states = new Array();

  states[0] = new Array('Alberta','British Columbia','Ontario');
  states[1] = new Array('Baja California','Chihuahua','Jalisco');
  states[2] = new Array('California','Florida','New York');

  // Province lists
var provinces = new Array();

  provinces[0] = new Array();
  provinces[0][0] = new Array("Province1", "Province2");

// City lists
var cities = new Array();
  cities[0][0] = new Array();
  cities[0][0][0] = new Array('Edmonton','Calgary');
  cities[0][0][1] = new Array('Victoria','Vancouver');


function setStates(){

  cntrySel = document.getElementById('country');
  stateList = states[cntrySel.selectedIndex];

  changeSelect('state', stateList);
  setProvinces();

}

function setProvinces(){

  cntrySel = document.getElementById('country');
  stateSel = document.getElementById('state');
  provinceList = provinces[cntrySel.selectedIndex][stateSel.selectedIndex];

  changeSelect('province', provinceList);
  setCities();

}

function setCities(){

  cntrySel = document.getElementById('country');
  stateSel = document.getElementById('state');
  provinceList = document.getElementById('province');
  cityList = cities[cntrySel.selectedIndex][stateSel.selectedIndex][provinceList.selectedIndex];

  changeSelect('city_town_district', cityList);

}

function changeSelect(fieldID, newList) {

  selectField = document.getElementById(fieldID);
  selectField.options.length = 0;

  for (i=0; i<newList.length; i++) {
    selectField.options[selectField.length] = new Option(newList[i], newList[i], newList[i]);
  }
}

</script>

</head>

<body onload="setStates();">
<form name="test">

Country: 
<select name="country" id="country" onchange="setStates();">
  <option value="Canada">Canada</option>
  <option value="Mexico">Mexico</option>
  <option value="United States">United States</option>
</select>
<br>

State: 
<select name="state" id="state" onchange="setProvinces();">
  <option value="">Please select a State</option>
</select>
<br>

Province: 
<select name="province" id="province" onchange="setCities();">
  <option value="">Please select a Province</option>
</select>
<br>

City: 
<select name="city_town_district"  id="city_town_district">
  <option value="">Please select a City</option>
</select>
</form>
</body>
</html>

任何帮助将不胜感激!预先感谢!

Any help will be much appreciated! Thanks in advance!

推荐答案

旧问题,一个答案,每个人有时都必须这样做.今年是2018年,我找到了一个简单的方法来做到这一点.易于添加,编辑和接收来自服务器的数据.再见! JSFiddle

Old question, one answer and everybody have to do this sometimes. The year is 2018 and I find a easy way to do this. Easy to add, edit and receive data from server. Bye! JSFiddle

<!DOCTYPE html>
<html>

<head>
</head>

<body>
    <select name="slc1" id="slc1" onchange="_1sel();"></select>
    <select name="slc2" id="slc2" onchange="_2sel();"></select>
    <select name="slc3" id="slc3" onchange="_3sel();"></select>
    <select name="slc4" id="slc4"></select>
    <script>
    //just call the method when the select change
        var slc1 = document.getElementById("slc1");
        var slc2 = document.getElementById("slc2");
        var slc3 = document.getElementById("slc3");
        var slc4 = document.getElementById("slc4");

        var dataObj = {//is a valid json
            "brazil": {
                "sp": {
                    "sp1": ["sp1A", "sp1B"],
                    "sp2": ["sp2A"]
                },
                "mg": {
                    "mg1": ["mg1A", "mg1B"],
                    "mg2": ["mg2A", "mg1B"]
                },
                "rj": {
                    "rj1": ["rj1A", "rj1B", "rj1C"],
                    "rj2": ["rj2A", "rj1B"]
                }
            },
            "usa": {
                "ny": {
                    "ny1": ["ny1A","ny1B","ny1C"],
                    "ny2": ["ny2A"]
                },
                "tx": {
                    "tx1": ["tx1A"]
                }
            },
            "uk": {
                "ld": {
                    "ld1": ["ld1A","ld1B","ld1C"],
                    "ld2": ["ld2A"]
                },
                "hm": {
                    "hm1": ["hm1A", "hm1B"]
                }
            }
        }

        let ctrs = Object.keys(dataObj);//countries - Object.keys return an array
        cmo(ctrs, slc1);//push values to select one
        _1sel();//call the first method of chain
        //slc1.value = "usa"
        //slc1.dispatchEvent(new Event('change'));

        function _1sel() {
            slc2.innerHTML = "";//clear the target select
            let cities = Object.keys(dataObj[slc1.value]);//get the cities from country as array
            cmo(cities, slc2);//push values to select two
            _2sel();//call the second method of chain
        }

        function _2sel() {
            slc3.innerHTML = "";//clear
            let zones = Object.keys(dataObj[slc1.value][slc2.value]);//get the zones from country and city as array
            cmo(zones, slc3);//push values to select three
            _3sel();//call the third method of chain
        }

        function _3sel() {
            slc4.innerHTML = "";//clear
            let staffs = dataObj[slc1.value][slc2.value][slc3.value];//no Obj.keys here, just get the array of values
            cmo(staffs, slc4);//push values to select four
        }

        function cmo(arr, s) {//create options and add to select
            arr.forEach(o => {
                let opt = document.createElement("option");
                opt.value = o;
                opt.innerHTML = o;
                s.add(opt);
            });
        }
    </script>

</body>

</html>

这篇关于Javascript链下拉列表选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆