原生JS实现ajax省市区三联

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了原生JS实现ajax省市区三联相关的知识,希望对你有一定的参考价值。

<!-- /**
* @fileName: linkageUI.class.php
* @author: wk
* @DateTime: 2017/10/29 17:25
* @Description:
*/ -->

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta content="width=device-width, initial-scale=1.0" name="viewport">
<meta content="ie=edge" http-equiv="X-UA-Compatible">
<title>
Document
</title>
</meta>
</meta>
</meta>
</head>
<body>
省:
<select id="province" name="">
<option value="">
请选择
</option>
</select>
市:
<select id="city" name="">
<option value="">
请选择
</option>
</select>
区/县:
<select id="district" name="">
<option value="">
请选择
</option>
</select>
</body>
</html>
<script type="text/javascript">
var province = document.getElementById("province");
var city = document.getElementById("city");
var district = document.getElementById("district");

window.onload = function(){
change("",province);
};

province.onchange = function(){
if(province.value != ""){
change(province.value,city);
}

city.options.length = 1;
district.options.length = 1;
};

city.onchange = function(){
if(city.value != ""){
change(city.value,district);
}

district.options.length = 1;
};

function change(objFvalue,objC){
var res = new XMLHttpRequest();
// res.open("GET","linkageDemo.php?code=" + objFvalue);
res.open("POST","linkageDemo.php");
res.onreadystatechange = function(){
if(res.readyState == 4 && res.status == 200){
var str = res.responseText;
if(str){
objC.options.length = 1;
createOption(str,objC);
}
}
};
res.setRequestHeader(‘Content-Type‘,‘application/x-www-form-urlencoded‘);
res.send("code="+objFvalue);
}

function createOption(str,obj){
var row = str.split(‘|‘);
for(var i in row){
arr = row[i].split(‘^‘);
var option = document.createElement("option");
option.value = arr[0];
option.text = arr[1];
console.log(option.text);
obj.appendChild(option);
}
}
</script>




















































































以上是关于原生JS实现ajax省市区三联的主要内容,如果未能解决你的问题,请参考以下文章

原生js原生js的省市区三级联动

原生js实现Ajax

原生JS实现Ajax

原生JS实现Ajax

原生态JS操作ajax

原生js实现ajax