js省市级联实现。

for [element] in [collection] 用于循环下标,常用于json

for(index in arr){
console.info("下标:"+index+"值:"+arr[index]);
} =====================分割线========================
<html>
<head>
<meta charset="UTF-8">
<title>基于JSON级联列表</title>
<script type="text/javascript">
//JSON:主要用于数据交换,其次json就是有格式的字符串
//var str = "{'name':'demo','age':'18'}";
//这样写符合JSON语法(字符串用双引号)
var str = '{"name":"demo","age":"18"}';
console.info("当前类型为:"+typeof(str));
var obj = JSON.parse(str);
console.info("当前类型为:"+typeof(obj));
//JSON输出访问的两种方式
console.info(obj.name+","+obj.age+"||"+obj['name']+","+obj['age']);
str=JSON.stringify(obj);
console.info("当前类型为:"+typeof(str)); //采用JSON格式来存储相应的数据和编码
var proJSON = {"1":"广东省","2":"湖北省"};
var cityJSON = {"1":{"020":"广州","0755":"深圳","0756","珠海"},"2":{"027":"武汉","0710":"襄樊","0715":"赤壁"}} window.onload=function(){
var province = document.getElementById("province");
for(var temp in proJSOM){
province.add(new Option(proJSON[temp],temp));
}
} function setCity(){
//只要触发了此事件,则二级菜单必须还原
var city=document.getElementById('city');
city.Option.length=1;
//获取被选中省会的值
var val = document.getElementById('province').value;
console.info("cityJSON:"+cityJSON[val]);
if(!cityJSON[val]){
return;
}
//通过选中的值获取二级菜单的json数据
var sonJSON=cityJSON[val];
for(temp in sonJSON){
city.add(new Option(sonJSON[temp],temp))
} } </script>
</head> <body>
<!--数据的三种存储方式:
1:数据库
2:properties:存储非敏感数据,且key value 格式,省资源
3:XML:存储非敏感数据,且支持有结构 目前一般用于配置文件
4:硬编码:主要存储非敏感数据,且不改变的数据
-->
<select id="province" onChange="setCity()">
<option>--选择省会--</option>
</select>
<select id="city" >
<option>--选择城市--</option>
</select>
</body>
</html> =====================分割线======================== <html>
<head>
<meta charset="UTF-8">
<title>基于数组级联列表</title>
<script type="text/javascript">
//js中数组的下标支持中文
var arr = new Array();
arr['广东省'] = ['广州','深圳','珠海'];
arr['湖北省'] = ['武汉','襄樊','赤壁'];
window.onload=function(){
//向省会赋值下拉列表框
var proSel = document.getElementById("province");
for(var temp in arr){
proSel.add(new Option(temp,temp))
}
} function setCity(){
//清空当前下拉列表框的信息
var citySel = document.getElementById("city");
citySel.options.length=1;
//获取选中的省会信息
var pro = document.getElementById('province').value;
//如果选择获取提示信息则不执行
if(pro == ""){
return;
}
var citySel = document.getElementById('city');
for(var i=0;i<arr[pro].length;i++){
citySel.add(new Option(arr[pro][i],arr[pro][i]))
}
}
</script>
</head> <body>
<select id="province" onChange="setCity()">
<option>--选择省会--</option>
</select>
<select id="city" >
<option>--选择城市--</option>
</select>
</body>
</html>

js省市级联实现的更多相关文章

  1. 纯js实现省市级联效果

    我们都知道一般有注册的时候会让用户填入省市啊地区什么的,然后我就想使用纯js制作一个省市级联的效果,只是用于学习以及回顾温习用,首先看下效果图,界面很丑啊,不要嫌弃! 首先还是先看下我的项目目录吧 很 ...

  2. Ajax异步刷新省市级联

    省市级联在web前端用户注册使用非常广泛.Ajax异步刷新省市级联.如图:选择不同的区,自动加载相应的街. <TD class=field>位 置:</TD> <TD&g ...

  3. JavaScript 省市级联效果

    JavaScript 省市级联效果 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...

  4. 省市级联.net

    初学javascript,编译省市级联,使用json在一般处理程序中编译,利用ajax传递数据到web前台 <html xmlns="http://www.w3.org/1999/xh ...

  5. JS动态级联菜单

    JS动态级联菜单是前端常用的一个功能,特此抽时间研究了下,附上代码 <html> <head> <meta charset="utf-8" /> ...

  6. JavaScript实现省市级联效果实例

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  7. [JS]以下是JS省市联动菜单代码

    以下是JS省市联动菜单代码: 代码一: <html> <head> <title></title> <script language=" ...

  8. javascript中的省市级联效果

    学习javascript的时候都遇到过这样的需求,不仅是省市,还有其他的一些场景,看看关键的代码有哪些吧. <head runat="server"> <titl ...

  9. 全国省市级联数据sql语句 mysql版

    全国省市级联数据sql语句 mysql版 --省级 provincial create table provincial ( provincialID int, provincialName ), p ...

随机推荐

  1. 用户从输入URL到看到网页发生了什么?

    一.在浏览器地址栏输入URL 二.浏览器查看缓存1.如果资源未缓存,发起新请求如果已缓存,检验是否处于有效期,资源处于有效期内直接提供给客户端,否则与服务器进行验证.2.检验有效期通常有两个HTTP头 ...

  2. Oracle NET工作原理、配置及连接问题排查

    一.Oracle NET配置文件 Oracle NET是一个软件层,支持不同网络协议之间的转换.不同的物理机器可以借助这个软件层实现相互间的通信,具体而言就是实现对oracle的远程访问. oracl ...

  3. MyBatis Spring SqlSessionFactoryBean 配置

    在基本的 MyBatis 中,session 工厂可以使用 SqlSessionFactoryBuilder 来创建.而在 MyBatis-Spring 中,则使用 SqlSessionFactory ...

  4. 如何快速的将一个str转换为list

    # -*- coding: cp936 -*- #python 27 #xiaodeng #如何快速的将一个str转换为list str='python' print list(str)#['p', ...

  5. Ubuntu18.04中配置QT5.11开发环境

    准备工作 参考 https://wiki.qt.io/Install_Qt_5_on_Ubuntu . # 安装g++ sudo apt install build-essential # sudo ...

  6. SpringMVC multipart文件上传

    一.介绍   spring内建的multipart支持网络程序文件上传.我们可以通过配置MultipartResolver来启动上传支持.它定义在org.springframework.web.mul ...

  7. jsp之radio取值与赋值

    转自:https://blog.csdn.net/dongfengkuayue/article/details/50352937 取radio的值: js代码 $("input[name=' ...

  8. [Spring学习笔记 1 ] Spring 简介,初步知识--Ioc容器详解 基本原理。

    一.Spring Ioc容器详解(1) 20131105 1.一切都是Bean Bean可是一个字符串或者是数字,一般是一些业务组件. 粒度一般比较粗. 2.Bean的名称 xml配置文件中,id属性 ...

  9. C++11开发中的Atomic原子操作

    C++11开发中的Atomic原子操作 Nicol的博客铭 原文  https://taozj.org/2016/09/C-11%E5%BC%80%E5%8F%91%E4%B8%AD%E7%9A%84 ...

  10. 73条日常shell命令汇总,总有一条你需要!

    1.检查远程端口是否对bash开放: echo >/dev/tcp/8.8.8.8/53 && echo "open" 2.让进程转入后台: Ctrl + z ...