Home >Web Front-end >JS Tutorial >js province, prefecture and city cascade selection_javascript skills

js province, prefecture and city cascade selection_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:34:55980browse

demo1 is the simplest implementation. The page ID configuration needs to be written in a js file, which is suitable for simple situations
demo2 adds dynamic configuration and can pass in configuration items, which is suitable for pages with multiple cascading drop-downs
The following is the json format of prefecture and city data. It can be obtained using ajax or made into ashx/asmx service. It can also be saved directly as a js file. The format can be adjusted according to your prefecture and city data and the corresponding source code can be modified

Copy code The code is as follows:

var _ds_data=[
{
id:0,
name:" u5317u4EAC",
city:[
{
id:1,
name:"u5317u4EACu5E02",
area:[{id:1,name:"u4E1Cu57CEu533A"},{.. .},{...}...]
},...]
}

In order to prevent garbled characters, unicode encoding is used. The conversion code is as follows:
Copy code The code is as follows:

///
/// Change the original The string is converted to unicode in the format u....u....
///

public static string StringToUnicode(string srcText)
{
string dst = "";
char[] src = srcText.ToCharArray();
for (int i = 0; i < src.Length; i )
{
byte[] bytes = Encoding. Unicode.GetBytes(src[i].ToString());
string str = @"u" bytes[1].ToString("X2") bytes[0].ToString("X2");
dst = str;
}
return dst;
}
///
/// Convert the Unicode string u....u.... format word Convert string to original string
///

public static string UnicodeToString(string srcText)
{
string dst = "";
string src = srcText;
int len ​​= srcText.Length / 6;
for (int i = 0; i <= len - 1; i )
{
string str = "";
str = src .Substring(0, 6).Substring(2);
src = src.Substring(6);
byte[] bytes = new byte[2];
bytes[1] = byte.Parse (int.Parse(str.Substring(0, 2), NumberStyles.HexNumber).ToString());
bytes[0] = byte.Parse(int.Parse(str.Substring(2, 2), NumberStyles .HexNumber).ToString());
dst = Encoding.Unicode.GetString(bytes);
}
return dst;
}

http ://xiazai.jb51.net/201002/yuanma/cityselector.rar
Package download address
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn