Code JS encapsulation Ajax cascade drop-down list

Source: Internet
Author: User
1 // trigger all links when a drop-down list changes. (warning: Do not use circular dependencies between the drop-down lists.) 2 // This function complies with the following specifications: no memory data is used (all data is used in real time, real-time retrieval, and real-time data), and registration is not initialized. 3 // This specification can cause any exceptions in HTML, maintain the best stability 4 function refreshlinkage (ddlctrl) {5 var curid =! Ddlctrl? "": Ddlctrl. ID; 6 // If (curid = undefined | curid. replace ("",""). length <= 0) return; // If the refresh control is not specified, refresh all controls 7 // var curvalue = ddlctrl. value; // after triggering, let the child control find the desired value: the current value is 8 9 If (curid & ddlctrl) {10 var hfvaluectrlid = $ (ddlctrl ). ATTR ("valuectrlid"); 11 if (! Hfvaluectrlid) hfvaluectrlid = "HF _" + curid; 12 $ ("#" + hfvaluectrlid ). val ($ (ddlctrl ). val (); 13} 14 15 $ ("select "). each (function (index, ELEM) {16 if (! Curid | ELEM. ID! = Curid) {// refresh all or do not link yourself 17 var parentid = $ (ELEM). ATTR ("parentid"); 18 if (parentid! = Undefined & ("" + parentid ). replace ("",""). length> 0) {19 var parentids = parentid. split (','); // split parent ID 20 if (! Curid | parentids. indexof (curid)> = 0) {// If a control is linked by the current control 21 // The operation is linked to the subcontrol 22 refreshoptions (ELEM ); 23} 24} 25} 26}); 27 28 // refresh the specified drop-down list 29 function refreshoptions (ELEM) {30 if (! ELEM |! ELEM. ID) return; 31 32 var parentid = $ (ELEM ). ATTR ("parentid"); 33 var parentkey = $ (ELEM ). ATTR ("parentkey"); 34 if (! Parentid |! Parentkey) return; 35 36 var parentids = parentid. Split (','); 37 var parentkeys = parentkey. Split (','); 38 39 if (parentkeys. length! = Parentids. Length) 40 throw new error ("linkage select \" "+ ELEM. ID +" \ ": 'parentkey' and 'parentid' length is not same! "); 41 42 var elemvalueid = $ (ddlctrl). ATTR (" valuectrlid "); 43 If (! Elemvalueid) elemvalueid = "HF _" + ELEM. ID; 44 45 46 // clear the drop-down list 47 if ($ (ELEM ). find ("option "). length> 0) 48 $ ("#" + elemvalueid ). val (''); 49 $ (ELEM ). find ("option "). remove (); 50 51 // create the default row 52 var withempty = $ (ELEM ). ATTR ("withempty ")! = "False"; 53 var emptyvalue = ($ (ELEM ). ATTR ("emptyvalue") | ''); 54 var emptytext = ($ (ELEM ). ATTR ("emptytext") | (getsettingvalue ("isenglish", true ))? "Please select": "select"); 55 if (withempty) {56 $ (ELEM ). append ("<option value = '{v}'> {t} </option> ". replace ("{v}", emptyvalue ). replace ("{t}", emptytext); 57} 58 59 60 // the required data 61 var parentisempty = true; 62 var jsonstr = "{\" _ Action \ ": \" refreshselect \ ", \" _ selectctrl \ ": \" "+ ELEM. ID + "\", "; 63 for (var j = 0; j <parentkeys. length; j ++) {64 var parentvalue = ($ ("#" + parentids [J]). val () | ''); 65 jsonstr = jsonstr +" \ "" + parentkeys [J] + "\": \ "" + parentvalue + "\" "+ (j = parentkeys. length-1? "": ","); 66 If (parentvalue & parentvalue! = Emptyvalue) parentisempty = false; 67} 68 jsonstr = jsonstr + "}"; 69 70 71 // if no data exists at the parent level, no Ajax 72 If (parentisempty) is performed) return; 73 74 75 // Ajax submit 76 var jsondata = JSON. parse (jsonstr); 77 var posturl = ($ (ELEM ). ATTR ("posturl") | (window. location. href); // If the post address is not specified, it will point to its own page 78 posturl = posturl + (posturl. indexof ("? ")> = 0? ("& _ RD =" + math. Random ()):("? _ RD = "+ math. random (); // prevents caching 79 $. post (posturl, jsondata, 80 function (data, status) {81 // alert ("data:" + Data + "\ nstatus:" + status); 82 If (Data! = Undefined & data. length> 0) {83 // dynamically obtained row 84 var optiontemp = ($ (ELEM ). ATTR ("optiontemp") | "<option value = '{v}'> {t} </option>"); 85 $. each (data, function (itemindex, itemelem) {86 var html = perfectexpres (optiontemp, itemelem); 87 $ (ELEM ). append (HTML); 88}); 89 90 // assign the value of the hidden control to the drop-down list control 91 var $ valuectrl =$ ("#" + elemvalueid ); 92 if ($ valuectrl. length> 0) 93 $ (ELEM ). val ($ valuectrl. val (); 94 95 // The change event that triggers this changed drop-down list 96 refreshlinkage (ELEM); 97} 98}, "JSON "); 99} 100 101 // all attributes in the cyclic object, replacing the corresponding part in expres; 102 function perfectexpres (expres, object) {103 for (var key in object) {104 expres = expres. replace ("{" + key + "}", (object [Key] | ''); 105} 106 return expres; 107} 108}

 

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.