在页面布局时,经常会用到三级联动下拉列表,作为一个前端开发人员,你会用js写省市区的三级联动代码吗?这篇文章就给大家分享如何用js实现全国城市三级联动效果,有一定的参考价值,感兴趣的朋友可以参考一下。
制作全国城市三级联动需要用到很多JavaScript知识,比如:function(),document.getElementById(),if函数,数组等等,如有不清楚的小伙伴可以参考PHP中文网的相关文章,或者访问 JavaScript视频教程。
实例描述:用插件实现全国城市省市区三级联动效果,具体代码如下:
HTML部分:
<div> <h1>全国城市三级联动</h1> </div> <div> <div> <select></select> <select></select> <select></select> <script></script> <script>_init_area();</script> </div> <div></div> </div>
CSS部分:
body {
background: #EEEEEE;
margin: 0;
padding: 0;
font-family: "微软雅黑", Arial, Helvetica, sans-serif;
}
a {
color: #006600;
text-decoration: none;
}
a:hover {
color: #990000;
}
.top {
margin: 5px auto;
color: #990000;
text-align: center;
}
.info select {
border: 1px #993300 solid;
background: #FFFFFF;
}
.info {
margin: 5px;
text-align: center;
}
.info #show {
color: #3399FF;
}
.bottom {
text-align: right;
font-size: 12px;
color: #CCCCCC;
width: 1000px;
}
JavaScript部分:
<script>
var Gid = document.getElementById;
var showArea = function() {
Gid('show').innerHTML = "<h3>省" + Gid('s_province').value + " - 市" +
Gid('s_city').value + " - 县/区" +
Gid('s_county').value + ""
}
Gid('s_county').setAttribute('onchange', 'showArea()');
</script>
效果如图所示:
一个基于官方 Mailtrap Node.js SDK 构建的 ASE 技能,用于发送事务邮件并与 Mailtrap 沙盒或生产流程协作。它非常适合需要 API 驱动邮件投递、测试邮箱验证和结构化消息自动化的代理工作流。

插件area.js的代码如下所示,可以直接拷贝使用。
function Dsy() {
this.Items = {};
}
Dsy.prototype.add = function(id, iArray) {
this.Items[id] = iArray;
}
Dsy.prototype.Exists = function(id) {
if(typeof(this.Items[id]) == "undefined") return false;
return true;
}
function change(v) {
var str = "0";
for(i = 0; i 0 || !v) {
if(dsy.Exists(str)) {
ar = dsy.Items[str];
for(i = 0; i <p>以上给大家分享了JS实现全国城市省市区三级联动的代码,代码比较多,以前没有接触过的小伙伴一定要自己动手练习,看看插件能不能成功使用,希望这个教程可以帮助到你!</p><p>更多相关教程请访问 <a href="http://www.php.cn/course/26.html" target="_blank">JavaScript中文参考手册<br></a><br></p>










