第一步,我先设计了个access数据库,做了一张存储省市区字段的表,至于其他字段就没给出了,重点是在于地区的字段。。。
表的设计:<!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> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>检索地区</title> <style type="text/css"> .wzss{ margin:50px;} </style> </head> <body> <div class="wzss" id="jmls"> <div class="wzssbox"> <span style="float:left;">微站搜索:</span> <form name="formsea" action="index.asp#jmls" method="post"> <input class="wzss1" type="text" onfocus="if(this.value=='姓名'){this.value='';}" onblur="if(this.value==''){this.value='姓名';}" name="wxtxt"<% if wxtxt <> "" then %> value="<% =wxtxt %>"<% else %> value="姓名"<% end if %> /> <select class="wzss2" id="province" name="province" data-click="changeCity"> <option value ="0">省份选择</option> </select> <select class="wzss2" id="city" name="city" data-click="changeArea"> <option value ="0">城市选择</option> </select> <select class="wzss2" id="area" name="area"> <option value ="0">地区选择</option> </select> <input class="wzss3" type="submit" name="submit" value=" 搜索 " /> </form> </div> </div> </body> </html>
<script language="javascript"> $(document).ready(function(e) { //这个是下拉选中的时候触发事件 $("#province, #city").change(function(e) { var wxact = $(this).attr("data-click"); var wxid = $(this).children("option:selected").val(); fun_ajax(wxact, wxid, 0); }); function fun_ajax(action, value, selectedid){ //alert(action) $.ajax({ url : "getdata.asp", type : "POST", data : {"act" : action, "wxareaid" : value, "selectedid" : selectedid}, async: false, success : function(msg){ //alert(msg) var data = msg.split("|||"); if(data[0] == 0){ $("#province").empty(); //清空下拉框先 $("#province").append(data[1]); } else if(data[0]==1){ $("#city").empty(); //清空下拉框先 $("#city").append(data[1]); }else if(data[0]==2){ $("#area").empty(); $("#area").append(data[1]); } }, error: function(){ alert('请选择省份'); } }); } //初始化下拉框 var selectedid1 = <% =wxarea1 %>; //province var selectedid2 = <% =wxarea2 %>; //city var selectedid3 = <% =wxarea3 %>; //area fun_ajax("selectedProvince", 0, selectedid1); if(selectedid2 > 0){ fun_ajax("selectedCity", selectedid1, selectedid2); } if(selectedid3 > 0){ fun_ajax("selectedArea", selectedid2, selectedid3); } }); </script>
if wxareaid = 0 and act = "selectedProvince" then sql = "select distinct AreaName1, Areaid1 from [Area] where AreaName1 <>''" str = "0|||<option value ='0'>省份选择</option>" else if act = "changeCity" or act = "selectedCity" then sql = "select distinct AreaName2, Areaid2 from [Area] where Areaid1="& wxareaid &" and AreaName2 <> ''" str = "1|||<option value ='0'>城市选择</option>" end if if act = "changeArea" or act = "selectedArea" then sql = "select distinct AreaName3, Areaid3 from [Area] where Areaid2="& wxareaid &" and AreaName3 <> ''" str = "2|||<option value ='0'>地区选择</option>" end if end if
if not rs.eof then selected = "" while not rs.eof if selectedid = 0 then selected = "" else if format_id(selectedid) = format_id(rs(1)) then selected = " selected='selected'" else selected = "" end if end if str = str & "<option value='"& rs(1) &"' "& selected &">"& rs(0) &"</option>" rs.movenext wend end if
而选中状态则是直接通过传过来的selectedid来和areaid对比得到。。
最终演示结果:
到这里,就是整个下拉框触发和选中的写法了,源代码我已经传到附件上,具体点击下面:
http://download.csdn.net/detail/eadio/8059797