var xmlHttp;
var zcq=0;
//自动加载国家
function country() {
var text = "sql=SELECT co_name FROM COUNTRY order by nvl(length(trim(co_name)),0) asc,co_name";
var url = "ajax/ajax!selectName.action";
createXmlhttp();
if (xmlHttp) {
xmlHttp.open("POST", url, true);
xmlHttp.setRequestHeader("Cache-Control", "no-cache");
xmlHttp.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded");
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
parseMessage('country');
}
}
xmlHttp.send(text);
}
}
//实现一级联动省份
function getPronvice() {
var country = document.getElementById("country").value;
var url = "ajax/ajax!selectName.action";
var text = "sql=SELECT pr_name FROM pronvice pr,country co WHERE co.co_id=pr.co_id and co.co_name='"
+ country + "' order by nvl(length(trim(pr_name)),0) asc,pr_name";
createXmlhttp();
if (xmlHttp) {
xmlHttp.open("POST", url, true);
xmlHttp.setRequestHeader("Cache-Control", "no-cache");
xmlHttp.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded");
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
if(zcq==0){
createSelect('pronvice', '2', 'getCity()');
zcq=1;
}
parseMessage('pronvice');
}
}
xmlHttp.send(text);
}
}
//实现二级联动城市
function getCity() {
var pronvice = document.getElementById("pronvice").value;
var url = "ajax/ajax!selectName.action";
var text = "sql=SELECT ci_name FROM city ci,pronvice pr WHERE ci.pr_id = pr.pr_id and pr.pr_name='"
+ pronvice + "' order by nvl(length(trim(ci_name)),0) asc,ci_name";
createXmlhttp();
if (xmlHttp) {
xmlHttp.open("POST", url, true);
xmlHttp.setRequestHeader("Cache-Control", "no-cache");
xmlHttp.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded");
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
if(zcq==1){
createSelect('city', '3', 'getArea()');
zcq=2;
}
parseMessage('city');
}
}
xmlHttp.send(text);
}
}
//实现三级联动区县
function getArea() {
var city = document.getElementById("city").value;
var url = "ajax/ajax!selectName.action";
var text = "sql=SELECT ar_name FROM city ci,area ar WHERE ci.ci_id=ar.ci_id and ci.ci_name='"
+ city + "' order by nvl(length(trim(ar_name)),0) asc,ar_name";
createXmlhttp();
if (xmlHttp) {
xmlHttp.open("POST", url, true);
xmlHttp.setRequestHeader("Cache-Control", "no-cache");
xmlHttp.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded");
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
if(zcq==2){
createSelect('area', '4', '');
zcq=3;
}
parseMessage('area');
}
}
xmlHttp.send(text);
}
}
//创建xmlHttp对象
function createXmlhttp() {
if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
} else if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
//自动创建创建下拉菜单方法
function createSelect(iid, id, methodName) {
var select = document.createElement("select");
select.style.cssText = "text-align: center";
select.id = iid;
document.getElementById(id).appendChild(select);
var obj = document.getElementById(iid);
obj.setAttribute("onchange", methodName);
}
//动态解析生成下拉菜单
function parseMessage(id) {
var xmlDoc = xmlHttp.responseXML.documentElement;
var xSel = xmlDoc.getElementsByTagName('root');
var select_root = document.getElementById(id);
select_root.options.length = 0;
for ( var i = 0; i < xSel.length; i++) {
var xValue = xSel[i].childNodes[0].firstChild.nodeValue;
var xText = xSel[i].childNodes[0].firstChild.nodeValue;
var option = new Option(xText, xValue);
select_root.add(option);
}
}