JQuery JSON implementation of no-refresh three-level linkage case study _jquery

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

<asp:dropdownlist id= "DDL1" runat= "Server" width= "100px" ></asp:DropDownList>
<asp:dropdownlist id= "DDL2" runat= "Server" width= "100px" ></asp:DropDownList>
<asp:dropdownlist id= "DDL3" runat= "Server" width= "100px" ></asp:DropDownList>

Js:
Copy Code code as follows:

<script src= "Js/jquery-1.4.2.min.js" type= "Text/javascript" ></script>
<script type= "Text/javascript" >
$ (document). Ready (function () {
Geta ();
$ ("#ddl1"). Change (function () {GETB ();});
$ ("#ddl2"). Change (function () {getc ();});
});
function Geta ()
{
$ ("#ddl1"). HTML ("");
$ ("#ddl1"). Append ("<option value= '-1 ' selected= ' selected ' > Please select ...</option>");
$ ("SELECT[NAME$=DDL1] > option:selected"). Remove ();
var Strid = 0;
$.getjson ("loadclass.ashx?ddlid=" + Strid, function (data) {
for (var i = 0; i < data.length; i++) {
$ ("SELECT[NAME$=DDL1]"). Append ($ ("<option></option>"). Val (data[i].id). HTML (data[i). Cname));
};
Getb ();
});
}
function Getb ()
{
$ ("#ddl2"). HTML (""); $ ("#ddl3"). HTML ("");
var Strid = $ ("#ddl1"). attr ("value");
if (Strid!= 0) {
$.getjson ("loadclass.ashx?ddlid=" + Strid, function (data) {
for (var i = 0; i < data.length; i++) {
$ ("SELECT[NAME$=DDL2]"). Append ($ ("<option></option>"). Val (data[i].id). HTML (data[i). Cname));
};
Getc ();
});
}
}
function getc ()
{
$ ("#ddl3"). HTML ("");
var Strid = $ ("#ddl2"). attr ("value");
if (Strid!= 0) {
$.getjson ("loadclass.ashx?ddlid=" + Strid, function (data) {
for (var i = 0; i < data.length; i++) {
$ ("select[name$=ddl3]"). Append ($ ("<option></option>"). Val (data[i].id). HTML (data[i). Cname));
};
});
}
}
</script>

LOADCLASS.ASHX:
Copy Code code as follows:

<%@ WebHandler language= "C #" class= "LoadClass"%>
Using System;
Using System.Web;
Using System.Text;
Using System.Data;
public class Loadclass:ihttphandler {
public void ProcessRequest (HttpContext context) {
Array [{"id": "275", "Cname": "A1"},{"id": "319", "Cname": "A2"},{"id": "322", "Cname": "A3"}]
int Strid = Convert.ToInt32 (context. request["Ddlid"]);
String strSQL = "SELECT * from Class where parent_ptr=" + Strid + "ORDER by classorder ASC";
DB D = new db ();
DataTable dt = D.GETDT (strSQL);
StringBuilder strclass = new StringBuilder ();
if (dt!= null)
{
Strclass.append ("[");
for (int i = 0; i < dt. Rows.Count; i++)
{
Strclass.append ("{");
Strclass.append ("\" id\ ": \" "+ dt.) rows[i]["id"]. ToString () + "\", ");
Strclass.append ("\" cname\ ": \" "+ dt.) rows[i]["Classcname"]. ToString () + "\" ");
if (i!= dt. ROWS.COUNT-1)
{
Strclass.append ("},");
}
}
}
Strclass.append ("}");
Strclass.append ("]");
Context. Response.ContentType = "Application/json";
Context. response.contentencoding = Encoding.UTF8;
Context. Response.Write (Strclass.tostring ());
Context. Response.End ();
}
public bool IsReusable {
get {
return false;
}
}
}

Attention:
Copy Code code as follows:

Background can only get value values, can not get text directly, need to pass JS, control relay
Results: 275 276 277
Label1.Text = Request.form[ddl1. UniqueID] + "" + request.form["DDL2"] + "" + request.form[ddl3. Clientid.replace ("_", "$")]; problems encountered: The value of the dropdown box text is passed through the HiddenField control to <asp:hiddenfield id= "HiddenField1" runat= "Server"/ >
<asp:hiddenfield id= "HiddenField2" runat= "Server"/>
<asp:hiddenfield id= "HiddenField3" runat= "Server"/>
Assign the value of the selected Drop-down box to the hidden control: <script type= "Text/javascript" >
var Key1 = $ ("#ddl1 >option:selected"). Val ();
$ (' #HiddenField1 '). Val (Key1);
var Key2 = $ ("#ddl2 >option:selected"). Val ();
$ (' #HiddenField2 '). Val (Key2);
var Key3 = $ ("#ddl3 >option:selected"). Val ();
$ (' #HiddenField3 '). Val (Key3);
</script>

The values that are dynamically assigned to the HiddenField control after you select the Drop-down box cannot correspond to the value that is selected by the Drop-down box!
It may have to do with initialization, where should the assignment of the code go? Or is there any good way to welcome the discussion?

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.