Home  >  Article  >  Web Front-end  >  JS makes address selection function

JS makes address selection function

php中世界最好的语言
php中世界最好的语言Original
2018-04-19 10:23:032071browse

This time I will bring you the JS address selection function. What are the precautions for JS address selection function? The following is a practical case, let’s take a look.​​ ​

<!DOCTYPE html>
<html>
  <head>
    <metacharset="UTF-8">
    <title></title>
    <script>
      var shenArr = new Array(5);
      shenArr["广东"] = ["广州","深圳","韶关","汕头","茂名"];
      shenArr["湖南"] = ["长沙","张家界","株洲","岳阳","吉首"];
      shenArr["湖北"] = ["武汉","宜昌","荆州","黄冈","仙桃"];
      shenArr["安徽"] = ["合肥","黄山"];
      shenArr["河南"] = ["郑州","开封","洛阳","信阳"];
       function getShen(){
        var s = document.getElementById("shen");
        for(var v in shenArr){
          //把数组键加入到省的下拉框
          s.add(new Option(v,v),null);
        }
       }
 
      function getCity(){
        var s = document.getElementById("shen");
        var c = document.getElementById("city");
        var v = s.value;//省份的名称,也是数组中的键
        c.options.length = 0;//把城市下拉框中的项清除
 
        //循环把某一个省的城市加入到市的下拉框
        for(var i in shenArr[v] ){
          c.add(new Option(shenArr[v][i],shenArr[v][i]),null);
        }
 
      }
 
    </script>
  </head>
  <bodyonload="getShen()">
    省:<selectid="shen"onchange="getCity()">
      <optionvalue="0">--请选择--</option>
    </select>
    市:<selectid="city"></select>
  </body>
</html>
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Real-time acquisition of the input input box content

vue implements the click-to-image magnification function (with code)

The above is the detailed content of JS makes address selection function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn