欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Ajax实现弹出式无刷新城市选择功能代码

程序员文章站 2023-11-09 19:34:28
本文实例讲述了ajax实现弹出式无刷新城市选择功能。分享给大家供大家参考。具体如下: 这是一款很棒的全国城市选择效果,添加城市时先添加组:找到id是"selectsub"...

本文实例讲述了ajax实现弹出式无刷新城市选择功能。分享给大家供大家参考。具体如下:

这是一款很棒的全国城市选择效果,添加城市时先添加组:找到id是"selectsub"中select标签下,添加option标签,value属性递增,找到id是"selectsub",按照原有格式添加div,其id属性递增;然后添加二级副选框选项:复制id是"selectsub"下任意input标签,粘贴在需要添加的位置即可,你想扩展的话,配合动态语言完全可以将城市从数据库中读取出来。

运行效果截图如下:

Ajax实现弹出式无刷新城市选择功能代码

在线演示地址如下:

具体代码如下:

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en"
"http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>ajax弹出式无刷新城市选择特效</title>
<meta http-equiv="content-type" content="text/html;charset=gb2312">
<style type="text/css">
body {
 font-size: 12px; padding-top: 50px
}
h2 {
 padding-right: 0px; padding-left: 0px; font-weight: bold; font-size: 12px; padding-bottom: 0px; margin: 0px; padding-top: 0px
}
.bton {
 border-right: #ccc 1px solid; border-top: #ccc 1px solid; background: #ddd; border-left: #ccc 1px solid; border-bottom: #ccc 1px solid
}
.cont {
 padding-right: 10px; padding-left: 10px; padding-bottom: 10px; padding-top: 10px
}
#main {
 margin: 0px auto; width: 400px
}
#selectitem {
 border-right: #000 1px solid; border-top: #000 1px solid; margin-top: 10px; z-index: 2; background: #fff; overflow: hidden; border-left: #000 1px solid; width: 400px; border-bottom: #000 1px solid; position: absolute; top: 0px
}
#preview {
 border-right: #ccc 1px solid; border-top: #ccc 1px solid; margin: 1px; border-left: #ccc 1px solid; border-bottom: #ccc 1px solid
}
#result {
 border-right: #ccc 1px solid; border-top: #ccc 1px solid; margin-top: 10px; border-left: #ccc 1px solid; border-bottom: #ccc 1px solid
}
.tit {
 padding-left: 10px; margin: 1px; line-height: 20px; height: 20px
}
.bgc_ccc {
 background: #ccc
}
.bgc_eee {
 background: #eee
}
.c_999 {
 color: #999
}
.pointer {
 cursor: pointer
}
.left {
 float: left
}
.right {
 float: right
}
.cls {
 clear: both; font-size: 0px; overflow: hidden; height: 0px
}
#bg {
 display: none; z-index: 1; background: #ccc; filter: alpha(opacity=70); left: 0px; width: 100%; position: absolute; top: 0px; opacity: 0.7
}
.hidden {
 display: none
}
.move {
 cursor: move
}
</style>
</head>
<body>
<div id="main">
 <input class="bton pointer" onclick="openbg(1);openselect(1)" type="button" value="请选择" name="button">
 <div id="result">
 <div class="tit bgc_eee">
 <h2>您已选择的城市汇总</h2>
 </div>
 <div class="cont" id="makesureitem"></div>
 </div>
</div>
<div id="bg"></div>
<div class="hidden" id="selectitem">
 <div class="tit bgc_ccc move" onmousedown="drag(event,this)">
 <h2 class="left">请选择城市:</h2>
 <span class="pointer right" onclick="openbg(0);openselect(0);">[取消]</span>
 <span class="pointer right" onclick="makesure();">[确定]</span>
 </div>
 <div class="cls"></div>
 <div class="cont">
 <div id="selectsub">
 <select style="margin-bottom: 10px" onchange="showselect(this.value)" name="">
 <option value="0" selected>中国名城</option> 
 <option value="1">魅力城市</option>
 <option value="2">东北三省</option>
 <option value="3">世界城市</option>
 </select> 
 <div id="c00">
 <input onclick="addpreitem()" type="checkbox" value="北京" name="ck00">北京
 <input onclick="addpreitem()" type="checkbox" value="福建" name="ck00">福建
 <input onclick="addpreitem()" type="checkbox" value="四川" name="ck00">四川
 <input onclick="addpreitem()" type="checkbox" value="江苏" name="ck00">江苏
 </div>
 <div id="c01">
 <input onclick="addpreitem()" type="checkbox" value="上海" name="ck01">上海
 <input onclick="addpreitem()" type="checkbox" value="云南" name="ck01">云南
 <input onclick="addpreitem()" type="checkbox" value="贵州" name="ck01">贵州
 </div>
 <div id="c02">
 <input onclick="addpreitem()" type="checkbox" value="黑龙江" name="ck01">黑龙江
 <input onclick="addpreitem()" type="checkbox" value="吉林" name="ck01">吉林
 <input onclick="addpreitem()" type="checkbox" value="辽宁" name="ck01">辽宁
 </div>
 <div id="c03">
 <input onclick="addpreitem()" type="checkbox" value="美国" name="ck01">美国
 <input onclick="addpreitem()" type="checkbox" value="阿富汗" name="ck01">阿富汗
 <input onclick="addpreitem()" type="checkbox" value="日本" name="ck01">日本
 </div>
 </div>
 </div>
 <div id="preview">
 <div class="tit bgc_eee c_999">
 <h2>您已选择:</h2>
 </div>
 <div class="cont" id="previewitem"></div>
 </div>
</div>
<script type="text/javascript">
var grow = $("selectsub").getelementsbytagname("option").length; //组数
var showgrow = 0;//已打开组
var selectcount = 0; //已选数量 
showselect(showgrow);
var items = $("selectsub").getelementsbytagname("input");
function $(o){
 if(typeof(o) == "string")
 return document.getelementbyid(o);
 return o;
}
function openbg(state){
 if(state == 1)
 {
  $("bg").style.display = "block";
  var h = document.body.offsetheight > document.documentelement.offsetheight ? document.body.offsetheight : document.documentelement.offsetheight;
  $("bg").style.height = h + "px";
 }
 else
 {
  $("bg").style.display = "none";
 } 
}
function openselect(state){
 if(state == 1) 
 {
  $("selectitem").style.display = "block";
  $("selectitem").style.left = ($("bg").offsetwidth - $("selectitem").offsetwidth)/2 + "px";
  $("selectitem").style.top = document.body.scrolltop + 100 + "px";  
 }
 else
 {
  $("selectitem").style.display = "none";
 }
}
function showselect(id){
 for(var i = 0 ; i < grow ;i++)
 {
  $("c0" + i).style.display = "none";
 }
 $("c0" + id).style.display = "block";
 showgrow = id;
}
function open(id,state){ 
 if(state == 1)
 $(id).style.display = "block";
 $(id).style.diaplay = "none";
}
function addpreitem(){ 
 $("previewitem").innerhtml = "";
 var len = 0 ;
 for(var i = 0 ; i < items.length ; i++)
 {
  if(items[i].checked == true)
  {
   var mes = "<input type='checkbox' checked='true' value='"+ items[i].value +"' onclick='copyitem(\"previewitem\",\"previewitem\");same(this);'>" + items[i].value;
   $("previewitem").innerhtml += mes;
  }
 }
}
function makesure(){
 openbg(0);
 openselect(0);
 copyitem("previewitem","makesureitem") 
}
function copyhtml(id1,id2){
 $(id2).innerhtml = $("id1").innerhtml;
}
function copyitem(id1,id2){
 
 var mes = "";
 var items2 = $(id1).getelementsbytagname("input");
 for(var i = 0 ; i < items2.length ; i++)
 {
  if(items2[i].checked == true)
  {
  mes += "<input type='checkbox' checked='true' value='"+ items2[i].value +"' onclick='copyitem(\"" + id2+ "\",\""+ id1 +"\");same(this);'>" + items2[i].value;   
  }
 }
 $(id2).innerhtml = "";
 $(id2).innerhtml += mes;
}
function same(ck){
 for(var i = 0 ; i < items.length ; i++)
 {
  if(ck.value == items[i].value)
  {
   items[i].checked = ck.checked;
  }
 }
} 
var odrag = "";
var ox,oy,nx,ny,dy,dx;
function drag(e,o){
 var e = e ? e : event;
 var moused = document.all ? 1 : 0;
 if(e.button == moused)
 {
  odrag = o.parentnode;
  ox = e.clientx;
  oy = e.clienty;  
 }
}
function dragpro(e){
 if(odrag != "")
 { 
  var e = e ? e : event;
  dx = parseint($(odrag).style.left);
  dy = parseint($(odrag).style.top);
  nx = e.clientx;
  ny = e.clienty;
  $(odrag).style.left = (dx + ( nx - ox )) + "px";
  $(odrag).style.top = (dy + ( ny - oy )) + "px";
  ox = nx;
  oy = ny;
 }
}
document.onmouseup = function(){odrag = "";}
document.onmousemove = function(event){dragpro(event);}
</script>
</body>
</html>

希望本文所述对大家的ajax程序设计有所帮助。