js ajax擷取城市對應的行政地區、商業地區、地標

來源:互聯網
上載者:User
 代碼如下 複製代碼

//------------------------------------------------------------
//------AJAX部分:擷取城市對應的行政地區、商業地區、地標------
//------------------------------------------------------------

//建立httpRequest對象
function createXMLHttpRequest(){
  var xmlhttp;
if (window.ActiveXObject){
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP")

}else if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}
return xmlhttp;
}

//發送ajax請求,用返回結果設定type代表的三個元素值
//type = 1(擷取行政地區)
//type = 2(擷取商業地區)
//type = 3(擷取地標)
//city (所屬城市)
function ajaxSetObj( type , city )
{
var xmlHttpObj = createXMLHttpRequest();
xmlHttpObj.onreadystatechange = function(){
if (xmlHttpObj.readystate == 4 && xmlHttpObj.status == 200){
 //用後台得到的結果設定html元素
 setHTML( type , xmlHttpObj.responseText );
}
  }
xmlHttpObj.open("POST","hotelQuery.aspx",false);
xmlHttpObj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlHttpObj.send( "type=" + type + "&city=" + city );
}
//設定ajax請求後的html元素
function setHTML( type , txt ){
setAdminArea( txt );
}
//設定行政地區
function setAdminArea( txt ){

var selectObj = document.getElementById( "selectAreaCode" );

//首先清空下拉框
clareSelectObj(selectObj);
//再賦值
var tempStrs = txt.split( "|" );
for( var i=0;i<tempStrs.length-1;i++ ){
selectObj.options.add( new Option( tempStrs[i],tempStrs[i] ) );
}
selectObj.style.display = "";
}
//清空下拉框
function clareSelectObj( selectObj ){
selectObj.innerHTML = "";
}
//ajax入口
//obj (所屬城市文字框)
function doAjax(){
var obj=document.getElementById("hotelCity");
if( obj.value == "" ){
return;
}
//設定行政地區下拉式清單
if(document.getElementById("radioAdminArea").checked)
{
document.getElementById( "hiddenHotelArea" ).value="1";
ajaxSetObj( 1 , obj.value );
}

//設定商業地區下拉式清單
if(document.getElementById("radioBusiness").checked)
{
document.getElementById( "hiddenHotelArea" ).value="2";
ajaxSetObj( 2 , obj.value );
}

//設定地標多選按鈕
if(document.getElementById("radioPlace").checked)
{
document.getElementById( "hiddenHotelArea" ).value="3";
ajaxSetObj( 3 , obj.value );
}
}
 function setAdminTxt( selectObj ){
document.getElementById( "hiddenHotelArea" ).value = selectObj.value;
}
function StartRequest(name)
{
var xmlHttpObj = createXMLHttpRequest();
xmlHttpObj.onreadystatechange = function(){
if (xmlHttpObj.readystate == 4 && xmlHttpObj.status == 200){

 //顯示內容
 for(var j=1;j<document.getElementById("drpcity").options.length;j=j)
{
document.getElementById("drpcity").options.remove(j);
}
showCity(xmlHttpObj.responseText);
}
  }
  
  var para = name;
  var aboutPara = "province="+para;
xmlHttpObj.open("POST","hotelAdd.aspx",false);
xmlHttpObj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlHttpObj.send( aboutPara );

}
function showCity(province)
{
//綁定城市資訊
var array=province.split(",");
//document.getElementById("drpcity").options.add(new Option("請選擇"));
for(var i=0;i<array.length;i++)
{
var drpCity=document.getElementById("drpcity");
var option=new Option(array[i].toString());
drpCity.options.add(option);
}

}
function getCity(province)
{
StartRequest(province);
}
 function getCityTxt()
  {
var txtcity=document.getElementById("hotelCity");
var drpCity=document.getElementById("drpcity");
txtcity.value = drpCity.options[drpCity.selectedIndex].text;
  }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.