Ajax implementation of two or three-level linkage drop-down box

Source: Internet
Author: User
Tags vars

$.ajax ({url:url + "Option=dropdown"),


Type: "Post",


Data: "znstr=" + str + "&znclass=" + Zclass + "",


DataType: "HTML",


Error:function (XMLHttpRequest) {},


Success:function (data) {


if (data!= "false")


{


if (data.tostring (). length > 0)


{


Cleardrop (Znclass);


}


var vars = new Array ();


VARs = Data.tostring (). Split (', ');


for (var i =0;i< (vars.length); i=i+2)


{


Switch (znclass)


{


Case 2:


$ ("#citytwo"). Append ("<option value= '" +vars[i+1]+ "' >" +vars[i]+ "</option>");


Break


Case 3:


$ ("#citythree"). Append ("<option value= '" +vars[i+1]+ "' >" +vars[i]+ "</option>");


Break


Case 4:


$ ("#cityfour"). Append ("<option value= '" +vars[i+1]+ "' >" +vars[i]+ "</option>");


Break


}





}


%>





Let's look at a complete example





<%@ page contenttype = "text/html; CHARSET=GBK "import=" JAVA.UTIL.*,COM.WEHAVE.HYERP.PROCUREMENT.DOMAIN.CGSQD "%>


<%@ taglib uri= "struts-html" prefix= "html"%>


<%@ taglib uri= "struts-logic" prefix= "logic"%>


<%@ taglib uri= "Struts-bean" prefix= "Bean"%>


<html>


<head>


<title> </title>


<link rel = "stylesheet" type = "Text/css tutorial" href = ". /css/olstyle.css ">


<script type= "text/web Effects" >


var req;


Window.onload=function () {


}





function Change_select ()


{


var Zhi=document.getelementbyid (' M_GYKFWZLBB_LBBM '). Value;


var url= "sqdselect.go?method=getskill&id=" +zhi;


if (zhi== "0") {


Alert ("Please select the information you want to view");


Return


}else{


if (window.xmlhttprequest)


{


Req=new XMLHttpRequest ();


}else if (window.activexobject)


{


Req=new ActiveXObject ("Microsoft.XMLHTTP");


}





if (req)


{


Req.open ("Get", url,true);


Req.onreadystatechange=callback;


Req.send (NULL);


}


}


}





function Change_select2 ()


{


var Zhi=document.getelementbyid (' M_lbbm_se '). Value;


Alert (zhi.substring (0,2));


if (zhi== "0") {


Alert ("Please select the information you want to view");


Return


}


if (zhi.substring (0,2) = = "WY") {


var url= "sqdselect.go?method=getskill2&id=" +zhi;


if (window.xmlhttprequest)


{


Req=new XMLHttpRequest ();


}else if (window.activexobject)


{


Req=new ActiveXObject ("Microsoft.XMLHTTP");


}





if (req)


{


Req.open ("Get", url,true);


Req.onreadystatechange=callback2;


Req.send (NULL);


}


}else{


Parent.topfram.location = "sqdselectall.go?method=getlistall&id=" +zhi;


}


}





function Change_select3 ()


{


var Zhi=document.getelementbyid (' m_lbbm_th '). Value;


Alert (zhi.substring (0,2));


if (zhi== "0") {


Alert ("Please select the information you want to view");


Return


}else{


Parent.topfram.location = "sqdselectall.go?method=getlistall2&id=" +zhi;


}


}





function callback ()


{


if (req.readystate = 4)


{


if (Req.status = 200)


{


Parsemessage ();


}else{


Alert ("Not able to retrieve description" +req.statustext);


}


}


}





function Callback2 ()


{


if (req.readystate = 4)


{


if (Req.status = 200)


{


Parsemessage2 ();


}else{


Alert ("Not able to retrieve description" +req.statustext);


}


}


}





function Parsemessage ()


{


var xmldoc=req.responsexml.documentelement;


var xsel=xmldoc.getelementsbytagname (' select ');


var Select_root=document.getelementbyid (' M_lbbm_se ');


select_root.options.length=0;





for (Var i=0;i<xsel.length;i++)


{


var Xvalue=xsel[i].childnodes[0].firstchild.nodevalue;


var Xtext=xsel[i].childnodes[1].firstchild.nodevalue;


var option=new option (Xtext,xvalue);


try{


Select_root.add (option);


}catch (e) {


}


}


}





function Parsemessage2 ()


{


var xmldoc=req.responsexml.documentelement;


var xsel=xmldoc.getelementsbytagname (' select ');


var Select_root=document.getelementbyid (' m_lbbm_th ');


select_root.options.length=0;





for (Var i=0;i<xsel.length;i++)


{


var Xvalue=xsel[i].childnodes[0].firstchild.nodevalue;


var Xtext=xsel[i].childnodes[1].firstchild.nodevalue;


var option=new option (Xtext,xvalue);


try{


Select_root.add (option);


}catch (e) {


}


}


}











</script>


</head>

<body bgcolor = "#c8d0d4" >


<html:form action= "/cgsqdnewaction.go" >


<table class = "Cbtoolbar" id = "Idtoolbar" cellpadding = ' 0 ' cellspacing = ' 0 ' >


<tr align = "left" valign = "Top" >


<TD nowrap>  Substance Category:  


<html:select property= "M_GYKFWZLBB_LBBM" onchange= "Change_select ()" >


<html:option value= "0" > Please select </html:option>


<html:options collection= "lbflist" property= "M_GYKFWZLBB_LBBM" labelproperty= "M_GYKFWZLBB_LBMC"/>


</html:select>  


<html:select property= "M_lbbm_se" styleid= "M_lbbm_se" onchange= "Change_select2 ()" >


<html:option value= "0" >   </html:option>


</html:select>


<html:select property= "m_lbbm_th" styleid= "m_lbbm_th" onchange= "Change_select3 ()" >


<html:option value= "0" >   </html:option>


</html:select>





</td>





</tr>


</table>


</html:form>


</body>


</html>

Action




Public Actionforward DOSELECTWZLB (


Actionmapping Mapping,


Actionform form,


HTTPS Tutorial Ervletrequest req,


HttpServletResponse Res) {


HttpSession session = Req.getsession ();


Usersession usersession =


(usersession) Session.getattribute ("Usersession");


Permission.setuserid (Usersession.getuserid ());


Permission.setusername (Usersession.getusername ());


Permission.setmodulename ("M_cgsqd");


Permission.setactionstr ("' M_select '");


if (permissionservice.checkuserpermission (permission)) {


int updatesign=0;


try{


CGSQDSERVICE.UPDATECGSQWZHZB_SQSL ();


Updatesign=1;


}catch (Exception e) {


E.printstacktrace ();


updatesign=0;


}


if (updatesign==1) {


List lbflist=cgsqdservice.treelistwzlb2 ();


Req.setattribute ("Lbflist", lbflist);


Return Mapping.findforward ("Tools");


}else{


return null;


}


}else{


Return Mapping.findforward ("failure");


}


}





/** *//**


*


* Query Material coding Category List operation (level two)


* */


Public Actionforward DOSELECTWZLB1 (


Actionmapping Mapping,


Actionform form,


HttpServletRequest req,


HttpServletResponse Res) {


HttpSession session = Req.getsession ();


Usersession usersession =


(usersession) Session.getattribute ("Usersession");


Permission.setuserid (Usersession.getuserid ());


Permission.setusername (Usersession.getusername ());


Permission.setmodulename ("M_cgsqd");


Permission.setactionstr ("' M_select '");


if (permissionservice.checkuserpermission (permission)) {


int updatesign=0;


try{


CGSQDSERVICE.UPDATECGSQWZHZB_SQSL ();


Updatesign=1;


}catch (Exception e) {


E.printstacktrace ();


updatesign=0;


}


if (updatesign==1) {


List lbflist=cgsqdservice.treelistwzlb2 ();


Req.setattribute ("Lbflist", lbflist);


Return Mapping.findforward ("TOOLS1");


}else{


return null;


}


}else{


Return Mapping.findforward ("failure");


}


}





/** *//**


*


* Query Material coding Category List operation (level three)


* */


Public Actionforward Getskill (


Actionmapping Mapping,


Actionform form,


HttpServletRequest req,


HttpServletResponse Res) {


HttpSession session = Req.getsession ();


Usersession usersession =


(usersession) Session.getattribute ("Usersession");


Permission.setuserid (Usersession.getuserid ());


Permission.setusername (Usersession.getusername ());


Permission.setmodulename ("M_cgsqd");


Permission.setactionstr ("' M_select '");


if (permissionservice.checkuserpermission (permission)) {


String Id=req.getparameter ("id");





Res.setcontenttype ("TEXT/XML;CHARSET=GBK");


Res.setheader ("Cache-control", "No-cache");


String xml_start= "<?xml version=" 1.0 "encoding=" GBK "?>";


xml_start+= "<selects>";


String xml_end= "</selects>";


String xml= "<select><value>0</value><text> Please select </text></select>";


String M_lbbm_se= "";


String M_lbmc_se= "";


List lbflist=null;


if (Id.equals ("WY")) {


Lbflist=cgsqdservice.treelistwzlb3_2 (ID);


}else{


Lbflist=cgsqdservice.treelistwzlb3_1 (ID);


}


Iterator It=lbflist.iterator ();


while (It.hasnext ()) {


CGSQD cgsqd= (CGSQD) it.next ();


M_LBBM_SE=CGSQD.GETM_GYKFWZLBB_LBBM ();


M_LBMC_SE=CGSQD.GETM_GYKFWZLBB_LBMC ();


XML + + <select><value> "+m_lbbm_se+" </value><text> "+m_lbmc_se+" </text></select > ";


}


String Last_xml=xml_start+xml+xml_end;


Logger.debug ("XML is:" +last_xml);


try {


Res.getwriter (). write (Last_xml);


catch (IOException e) {


E.printstacktrace ();


}


return null;


}else{


return null;


}


}

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.