JS Ajax access to the city's corresponding administrative regions, commercial areas, landmarks

Source: Internet
Author: User
The code is as follows Copy Code

//------------------------------------------------------------
------Ajax section: Get the city's corresponding administrative area, commercial areas, landmarks------
//------------------------------------------------------------

Creating HttpRequest objects
function Createxmlhttprequest () {
var xmlhttp;
if (window. ActiveXObject) {
XMLHTTP = new ActiveXObject ("Microsoft.XMLHTTP")

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

To send an AJAX request, set the value of three elements represented by the type with the return result
Type = 1 (get administrative area)
Type = 2 (Get business area)
Type = 3 (get landmark)
City (urban)
function Ajaxsetobj (type, city)
{
var xmlhttpobj = Createxmlhttprequest ();
Xmlhttpobj.onreadystatechange = function () {
if (xmlhttpobj.readystate = = 4 && xmlhttpobj.status = 200) {
Set HTML elements with results from the background
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);
}
To set HTML elements after an AJAX request
function sethtml (type, txt) {
Setadminarea (TXT);
}
Set up administrative areas
function Setadminarea (TXT) {

var selectobj = document.getElementById ("Selectareacode");

First clear the dropdown box
Clareselectobj (Selectobj);
And then assign a value
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 = "";
}
Clear the dropdown box
function Clareselectobj (selectobj) {
selectobj.innerhtml = "";
}
Ajax Portal
Obj (Owning City text box)
function Doajax () {
var Obj=document.getelementbyid ("Hotelcity");
if (Obj.value = = "") {
Return
}
Set up the Administrative area dropdown list
if (document.getElementById ("Radioadminarea"). Checked)
{
document.getElementById ("Hiddenhotelarea"). value= "1";
Ajaxsetobj (1, obj.value);
}

Set up a business area Drop-down list
if (document.getElementById ("radiobusiness"). Checked)
{
document.getElementById ("Hiddenhotelarea"). value= "2";
Ajaxsetobj (2, Obj.value);
}

//Set landmark multi-select button
if (document.getElementById ("Radioplace"). Checked)
{
document.getElementById (" Hiddenhotelarea "). value=" 3 ";
Ajaxsetobj (3, Obj.value);
}
}
 function Setadmintxt (selectobj) {
document.getElementById ("Hiddenhotelarea"). Value = Select Obj.value;
}
Function startrequest (name)
{
var xmlhttpobj = Createxmlhttprequest ();
Xmlhttpobj.onreadystatechange = function () {
if (xmlhttpobj.readystate = 4 && xmlhttpobj.status =) {

Display content
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)
{
Binding City Information
var array=province.split (",");
document.getElementById ("Drpcity"). Options.add (new option ("Please select");
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;
  }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.