Select cascade Based on JQuery

Source: Internet
Author: User

Copy codeThe Code is as follows:
$ (Document). ready (function (){


$ ("# Precinct"). change (function (){
$ ("# Ptype"). val (""); // reset all
$ ("# Stype" ).html ("");
$ ("# Stype"). append ("<option value = \" \ "> -- select -- </option> ");
});

<Span style = "white-space: pre"> </span> // listen to the change event of the patent type
$ ("# Ptype"). change (function (){
Var ptype = $ (this );
Var atype = $ (this). val (); // object Value
Var pid = $ ("# precinct"). val ();
If (! Ptype. data (atype) {// get the value from the cache and do not need to interact with the server.
$. Post ("Main/PatentSubsidy/getSubsidy", {askfor: atype, precinct: pid}, function (json) {// return a json object
$ ("# Stype" ).html (""); // clear <span style = "font-family: Arial, Helvetica, sans-serif; "> # stype </span> drop-down list
For (var I = 0; I <json. length; I ++ ){
// Add
$ ("# Stype "). append ("<option value = '" + json [I]. id + "'>" + json [I]. value + "</option> ");
};
Ptype. data (atype, json); // Add the key with the # ptype value to the cache
}, 'Json ');
} Else {
Var json = ptype. data (atype); // retrieves the cache
$ ("# Stype" ).html ("");
For (var I = 0; I <json. length; I ++ ){
// Add
$ ("# Stype "). append ("<option value = '" + json [I]. id + "'>" + json [I]. value + "</option> ");
};
}
});

});

Obtained according to # precinct and # ptype # stype

Action method
Copy codeThe Code is as follows:
Public void getSubsidy (){
String askfor = null, precinct = null;
If (null! = GetPara ("askfor ")&&! "". Equals (getPara ("askfor "))){
Askfor = getPara ("askfor ");
If (null! = GetPara ("precinct ")&&! "". Equals (getPara ("precinct "))){
Precinct = getPara ("precinct ");
}
} Else {
RenderJson ("[{\" id \ ": \" \ ", \" value \ ": \" -- select -- \ "}]"); // pass null values, return
}
String SQL = "select s. id, s. subsidy_type, p. name from org_subsidy_flow s, tab_precinct p where s. enabled = 'true' and p. status = '1' and s. patent_type = '"+ askfor +"' and s. precinct = p. id ";
If (null! = Precinct &&! "". Equals (precinct )){
SQL + = "and p. id =" + precinct;
}
SQL + = "order by p. id, s. id ";
List <Org_subsidy_flow> sf = Org_subsidy_flow.dao.find (SQL );
If (sf. size ()! = 0 ){
StringBuffer buffer = new StringBuffer ();
For (int I = 0; I <sf. size (); I ++ ){
Buffer. append ("{\" id \ ": \" "+ sf. get (I ). getInt ("id") + "\", \ "value \": \ "" + sf. get (I ). getStr ("subsidy_type") + "--" + sf. get (I ). getStr ("name") + "\"},");
}
If (buffer. length ()! = 0 ){
RenderJson ("[" + buffer. substring (0, buffer. length ()-1). toString () + "]");
}
} Else {
RenderJson ("[{\" id \ ": \" \ ", \" value \ ": \" -- select -- \ "}]");
}
}

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.