Table structure Second-level or multi-level linkage is mainly based on the table with parent number in the database, and this is no exception Three columns of id, parent_id, name.
Using js operation Let’s first talk about how data is stored in js. Mainly use two-dimensional arrays to store data. The structure is as follows: a[parent number]=[[child number 1, child name 1], [child number 2, child name 2], [child number 3, child name 3],…]; First, use the parent number to get all the child data, and then bind the number and name of the child data in dropdown
The first step is the second-level linkage data (how to get these data later)
//Get the corresponding child data based on the parent number and display it in the obj control //type=0 city, 1 school //pid parent number //obj to Dropdown to display data function SetRegions(type,pid,obj) { var text="
In the third step, when the city changes, select school data
//Initialize city school value //cityId city number //schoolId school number function InitRegions(cityId,schoolId) { //Initialize city data //SetRegions(0,"00000000-0000-0000-0000-000000000000","#cities"); //Find the city and set it as the default value $("#cities option[value =" cityId "]").attr("selected","selected"); if(schoolId!="00000000-0000-0000-0000-000000000001"){ //Initialize school data SetRegions(1,cityId,"#schools"); //Find a school and set it as the default value $("#schools option[value=" schoolId "]").attr("selected", "selected"); } }