JavaScript Ajax implementation of provincial urban linkage example

Source: Internet
Author: User


Summary of their own in the project to do the most is the provincial urban linkage selection.

One is the provincial urban style is the three select box, directly selected, the style is as follows:

One is the province select box exists, the city, District Select box dynamically added to the back (note the dynamic addition of elements, as well as the operation of future elements);

One is the mobile end of the whole large frame selection of provinces and cities, the style is as follows:

The possible style, and the implementation of the process is not the same, but the main ideas are the same. (some need to first give you the province's data, you can use Twig loop out, and other operations)

The general idea is: (1) Backstage need to write good access to the provincial urban area of three Ajax: such as

Shop_get_province_ajax:

path:/get_res_province_ajax

defaults: {_controller:shopbundle:forgetpassword: Get_res_province_ajax}

(2) write a good foreground style (the following simulation provinces and cities select box):

<div class= "Mid-box" >

<div class= "sel120fff fl ml10" >

<div class= "Sel50fffarrow" ></div >

<select class= "Provinceselect sel120" name= "res_province_id" >

<option value= "0" >-Please select-< /option>

</select>

</div>

<div class= "sel120fff fl ml10" >

<div class= " Sel50fffarrow "></div>

<select class=" Cityselect sel120 "name=" res_province_id ">

<option Value= "0" >-Please select-</option>

</select>

</div>

<div class= "sel120fff fl ml10" >

<div class= "Sel50fffarrow" ></div>

<select class= "Districtselect sel120" name= "Res_" province_id ">

<option value=" 0 ">-Please select-</option>

</select>

</div>

</div>


(3) Default, page loading complete, using the province Ajax request the province's data:

var chosetext = "<option value= ' 0 ' >-please select-</option>";

$(". Cityselect "). Append (Chosetext);

$(". Districtselect "). Append (Chosetext);

$.ajax ({

type: ' Get ',

URL: ' {{path (' Shop_get_province_ajax '
)}} ',

dataType: ' JSON

', Success:function (data) {

$ (". Cityselect "). empty ();

$(". Districtselect "). empty ();

$(". Cityselect "). Append (Chosetext);

$(". Districtselect "). Append (Chosetext);

var length=data.length;

for (Var i=0;i<length;i++) {

var region_option = ' <option id= ' finish_region_id_op ' +data[i].id+ ' ' value= ' + Data[i].id+ "' >" +data[i].name+ "</option>"

$ (". Provinceselect "). Append (region_option);

}

}

});


(4) When the province data is selected, the ID of the selected province is passed to the city's Ajax:

//Choice-appears City
$ ('. Provinceselect '). Change (function () {
$ ("). Cityselect "). empty ();
$(". Districtselect "). empty ();
$(". Cityselect "). Append (Chosetext);
$(". Districtselect "). Append (Chosetext);
var id = $ (". Provinceselect "). Val ();
var url= ' {path (' Shop_get_city_ajax ', {' id ': ' Text '}}} ';
url = url.replace ("text", id);
$.ajax ({
type: "Get",
Url:url,
data:{region:id},
DataType: "JSON",
success:function ( Data) {
var length=data.length;
for (Var i=0;i<length;i++) {
var city_option = "<option id= ' origin_city_id_op" +data[i].id+ "' value= '" +data[ I].id+ "' >" +data[i].name+ "</option>";
$(". Cityselect "). Append (city_option);
}
}
})
});


(4) When the zone data is selected, the ID of the selected zone is passed to the region's Ajax:

//select-Occurrence area
$ ('. Cityselect '). Change (function () {
$ ("). Districtselect "). empty ();
$(". Districtselect "). Append (Chosetext);
var id = $ (". Cityselect "). Val ();
var url = ' {path (' Shop_get_district_ajax ', {' id ': ' Text '}}} ';
url = url.replace ("text", id);
$.ajax ({
type: "Get",
Url:url,
data:{city:id},
DataType: "JSON",
success:function (data) {
var length=data.length;
For
(Var i=0;i<length;i++) {
var xian_option = ' <option id= ' origin_xian_id_op ' +data[i].id+ ' ' value= ' +data[i].id+ "' >" +data[i].name+ "</option>"
$ (". Districtselect "). Append (xian_option);
}
}
})
});

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.