省市级联
级联效果预览
1.测试页面index.html
- <html>
- <head>
- <title>PCC省市区级联</title>
- <script type="text/javascript" src="pcc.js"></script>
- <script type="text/javascript">
- load_xml(['province','city','county']);
- load_xml(['provinces','citys','countys']);
- </script>
- </head>
- <body>
- <div id="txtHint"></div>
- <select id="province"></select><select id="city"></select><select id="county"></select><br/>
- <select id="provinces"></select><select id="citys"></select><select id="countys"></select>
- </body>
- </html>
<html> <head> <title>PCC省市区级联</title> <script type="text/javascript" src="pcc.js"></script> <script type="text/javascript"> load_xml(['province','city','county']); load_xml(['provinces','citys','countys']); </script> </head> <body> <div id="txtHint"></div> <select id="province"></select><select id="city"></select><select id="county"></select><br/> <select id="provinces"></select><select id="citys"></select><select id="countys"></select> </body> </html>
2.脚本pcc.js
- /****************************************************************************************************
- JavaScript Document
- @author 陈小尼
- @version v1.2
- 加载xml文档
- 可以在IE6, IE5, IE7+, Firefox, Chrome, Opera, Safari浏览器运行
- 使用方法:
- 将代表省、市、区的select的id以数组的形式给load_xml()方法(如:load_xml(['province','city','county']))。
- 实例:
- <html>
- <head>
- <title>PCC省市区级联</title>
- <script type="text/javascript" src="pcc.js"></script>
- <script type="text/javascript">
- load_xml(['province','city','county']);
- </script>
- </head>
- <body>
- <button onclick="">LoadXml</button>
- <div id="txtHint"></div>
- <select id="province"></select><select id="city"></select><select id="county"></select>
- </body>
- </html>
- ***************************************************************************************************/
- var doc;
- function load_xml(a){
- var xmldoc;
- if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
- //alert(' code for IE7+, Firefox, Chrome, Opera, Safari');
- xmldoc=new XMLHttpRequest();
- }else {// code for IE6, IE5
- //alert(' code for IE6, IE5');
- xmldoc=new ActiveXObject("Microsoft.XMLHTTP");
- }
- xmldoc.onreadystatechange=function(){
- if (xmldoc.readyState==4){
- if( xmldoc.status==200){
- doc =xmldoc.responseXML;
- getProCiCo(a)
- }
- }
- }
- xmldoc.open("POST","GBT2260-1999.xml",true);
- xmldoc.send();
- }
- function getProCiCo(a){
- var province=document.getElementById(a[0]);
- var city=document.getElementById(a[1]);
- var county=document.getElementById(a[2]);
- //初始化级联框
- var pro=doc.getElementsByTagName("province");
- for(i=0;i<pro.length;i++){
- var oOption=new Option(pro[i].getAttribute('name'),pro[i].getAttribute('name'));
- province.options.add(oOption);
- if(province.value==pro[i].getAttribute('name')){
- var cit=pro[i].getElementsByTagName("city");
- for(j=0;j<cit.length;j++){
- var oOption=new Option(cit[j].getAttribute('name'),cit[j].getAttribute('name'));
- city.options.add(oOption);
- if(city.value==cit[j].getAttribute('name')){
- var cou=cit[j].getElementsByTagName('county');
- for(k=0;k<cou.length;k++){
- var oOption=new Option(cou[k].getAttribute('name'),cou[k].getAttribute('name'));
- county.options.add(oOption);
- }
- }
- }
- }
- }
- //省菜单onchange
- province.onchange=function(){
- city.options.length=null;
- county.options.length=null;
- for(i=0;i<pro.length;i++){
- if(province.value==pro[i].getAttribute('name')){
- var cit=pro[i].getElementsByTagName("city");
- for(j=0;j<cit.length;j++){
- var oOption=new Option(cit[j].getAttribute('name'),cit[j].getAttribute('name'));
- city.options.add(oOption);
- if(city.value==cit[j].getAttribute('name')){
- var cou=cit[j].getElementsByTagName('county');
- for(k=0;k<cou.length;k++){
- var oOption=new Option(cou[k].getAttribute('name'),cou[k].getAttribute('name'));
- county.options.add(oOption);
- }
- }
- }
- }
- }
- };
- //市菜单onchange
- city.onchange=function(){
- var cityval=city.value;
- city.options.length=null;
- for(i=0;i<pro.length;i++){
- if(province.value==pro[i].getAttribute('name')){
- var cit=pro[i].getElementsByTagName("city");
- for(j=0;j<cit.length;j++){
- var oOption=new Option(cit[j].getAttribute('name'),cit[j].getAttribute('name'));
- city.options.add(oOption);
- city.value=cityval;
- if(city.value==cit[j].getAttribute('name')){
- county.options.length=null;
- var cou=cit[j].getElementsByTagName('county');
- for(k=0;k<cou.length;k++){
- var oOption=new Option(cou[k].getAttribute('name'),cou[k].getAttribute('name'));
- county.options.add(oOption);
- }
- }
- }
- }
- }
- }
- } s