實現Ajax實現省市區三級級聯,需要Java解析json技術
整體Demo下載地址如下: 點我下載
address.html
<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Insert title here</title></head> <script type="text/javascript"> /** * 得到XMLHttpRequest對象 */ function getajaxHttp() { var xmlHttp; try { // Firefox, Opera 8.0+, Safari xmlHttp = new XMLHttpRequest(); } catch (e) { // Internet Explorer try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { alert("您的瀏覽器不支持AJAX!"); return false; } } } return xmlHttp; } /** * 發送ajax請求 * url--請求到服務器的URL * methodtype(post/get) * con (true(異步)|false(同步)) * functionName(回調方法名,不需要引號,這里只有成功的時候才調用) * (注意:這方法有二個參數,一個就是xmlhttp,一個就是要處理的對象) */ function ajaxrequest(url, methodtype, con, functionName) { //獲取XMLHTTPRequest對象 var xmlhttp = getajaxHttp(); //設置回調函數(響應的時候調用的函數) xmlhttp.onreadystatechange = function() { //這個函數中的代碼在什么時候被XMLHTTPRequest對象調用? //當服務器響應時,XMLHTTPRequest對象會自動調用該回調方法 if (xmlhttp.readyState == 4) { if (xmlhttp.status == 200) { functionName(xmlhttp.responseText); } } }; //創建請求 xmlhttp.open(methodtype, url, con); //發送請求 xmlhttp.send(); } window.onload=function(){ ajaxrequest("addressSerlvet?method=provincial","POST",true,addrResponse); } //動態獲取省的信息 function addrResponse(responseContents){ var jsonObj = new Function("return" + responseContents)(); for(var i = 0; i < jsonObj.addrList.length;i++){ document.getElementById('select').innerHTML += "<option value='"+jsonObj.addrList[i].id+"'>" +jsonObj.addrList[i].address+ "</option>" } } //選中省后 function pChange(){ //先將市的之前的信息清除 document.getElementById('selectCity').innerHTML="<option value='-1'>請選擇市</option>"; //再將區的信息清除 document.getElementById('selectArea').innerHTML="<option value='-1'>請選擇區</option>"; //再將用戶的輸入清楚 document.getElementById("addr").innerHTML=""; var val = document.getElementById('select').value; if(val == -1){ document.getElementById('selectCity')[0].selected = true; return; } //開始執行獲取市 ajaxrequest("addressSerlvet?method=city&provincial="+val,"POST",true,cityResponse); } //獲取市的動態數據 function cityResponse(responseContents){ var jsonObj = new Function("return" + responseContents)(); for(var i = 0; i < jsonObj.cityList.length;i++){ document.getElementById('selectCity').innerHTML += "<option value='"+jsonObj.cityList[i].id+"'>" +jsonObj.cityList[i].address+ "</option>" } } //選中市以后 function cChange(){ var val = document.getElementById('selectCity').value; //開始執行獲取區 ajaxrequest("addressSerlvet?method=area&cityId="+val,"POST",true,areaResponse); } //獲取區的動態數據 function areaResponse(responseContents){ var jsonObj = new Function("return" + responseContents)(); for(var i = 0; i < jsonObj.areaList.length;i++){ document.getElementById('selectArea').innerHTML += "<option value='"+jsonObj.areaList[i].id+"'>" +jsonObj.areaList[i].address+ "</option>" } } //點擊提交按鈕 function confirM(){ //獲取省的文本值 var p = document.getElementById("select"); var pTex = p.options[p.options.selectedIndex].text; if(p.value=-1){ alert("請選擇省"); return; } //獲取市的文本值 var city = document.getElementById("selectCity"); var cityTex = city.options[city.options.selectedIndex].text; if(city.value=-1){ alert("請選擇市"); return; } //獲取區的文本值 var area = document.getElementById("selectArea"); var areaTex = area.options[area.options.selectedIndex].text; if(area.value=-1){ alert("請選擇區"); return; } //獲取具體位置id文本值 var addr = document.getElementById("addr").value; //打印 document.getElementById("show").innerHTML = "您選擇的地址為 " + pTex + " " + cityTex + " " + areaTex + " " + addr; } </script><body> <select id="select" onchange="pChange()"> <option value="-1">請選擇省</option> </select> <select id="selectCity" onchange="cChange()"> <option value='-1'>請選擇市</option> </select> <select id="selectArea" onchange="aChange()"> <option value='-1'>請選擇市</option> </select> <input type="text" id="addr" /> <button onclick="confirM();">確定</button> <div id="show"></div></body></html>
新聞熱點
疑難解答
圖片精選