Examples of second-level interaction between jquery and AJAX (better Chinese processing)

Source: Internet
Author: User
Tags json

The so-called two-level linkage, in fact, a little bit of JS are not unfamiliar, about the implementation of the two-level linkage, not the use of JQ, but the original eco-JS Some, efficiency will be better, but personal feeling with jq write, the idea will be clear point.

Code: HTML section

<select name= "" id= "Province" >
<option> Please select </option>
<option value=1> Guangdong </option >
<option value=2> hunan </option>
</select>
<select name= "" id= "City" >
<option> Please select </option>
</select>
jq section
<script type= "Text/javascript" >
$ ( Document). Ready (function () {
$ ("#province"). Change (function () {
$.ajax ({
type: Post),
URL: " Index/city?id= "+$ (This). Val (),
datatype:" JSON ",
success:function (data) {
var sm = eval (data);
var str= ' <option> please choose </option> ';
for (i=0;i<sm.length;i++) {
str+= ' <option value= ' +sm[i].id+ ' > ' +sm[i].name+ ' </option> ';
}
$ ("#city"). html (str);
}}); </script>

After the province is selected, the ID of the province is passed to the city page, and then all the cities below it are queried from the database, and then the JSON data (data) is returned, for example, after selecting Guangdong, the JSON data format returned is:

[{"id": "3″," pid ":" 1″, "name": "\u5e7f\u5dde", "ename": "Guangzhou"},{"id": "4″," pid ":" 1″, "name": "\u6df1\u5733″," Ename ":" Shenz "},{" id ":" 5″, "pid": "1″," name ":" \u4e2d\u5c71″, "ename": "Www.111cn.net"}]

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.