JS plug-in (1)---AutoComplete asynchronous load dropdown data

Source: Internet
Author: User
Tags stringbuffer

Keep in mind the way you used the jquery autocomplete. 1. First define a text box

<input type= "hidden" id= "Statid" value= "-1" >   
<input type= "text" class= "Form-control graytips" id= " Statname "placeholder=" Please enter the site name/Pinyin First letter ">  
2. Script
     $ ("#statName"). AutoComplete ({source:function (request,response) {$.ajax ({  
                    Type: "POST", url: "<%=path%>/jsp/stat.do?action=getstatlist", DataType: "JSON", Cache:false, Async:false, data: {"Statname": encodeURI ($ ("#statName"). Val ())}, Su Ccess:function (JSON) {var data = eval (JSON);//json Array response ($.ma  
                            P (data,function (item) {var name = Item.name;  
                            var id = item.id; return {label:item.code+ '--' + item.name,//drop-down box display value Valu  
                          When e:item.name,//is selected, the value that is populated into the Drop-down box id:item.id//selected, and filled into the ID  }  
                        }));  
            }  
                }); delay:500,//delay 500ms facilitates input select:function (event, UI) {$ ("#statId"). Val (  
            Ui.item.id);   }  
        });
3, Background action returns JSON format data,
Get param asynchronously Load site drop-down list String statname = Urldecoder.decode (Request.getparameter ("statname") = = null?  
"": Request.getparameter ("Statname"). Trim (), "utf-8");  
Jsonarray Jsonarray = new Jsonarray ();  
Jsonobject jsonobject = new Jsonobject ();  
Statservice service = new Statservice ();  
List statlist = service.getstatlist (statname); if (statlist!= null) {for (int i = 0; i < statlist.size (); i++) {stationtable s = (Stationta  
        BLE) statlist.get (i);  
        Jsonobject.put ("id", S.getstatid ());  
        Jsonobject.put ("Name", S.getstatname ());  
        Jsonobject.put ("Code", S.getstatcode ());  
    Jsonarray.add (Jsonobject);  
} PrintWriter pw = null;  
StringBuffer sb = new StringBuffer ();  
Sb.append (Jsonarray.tostring ());  
    try {response.setcontenttype ("TEXT/HTML;CHARSET=GBK");  
    PW = Response.getwriter ();  
    Pw.write (Sb.tostring ());  
Pw.close ();  
catch (IOException e) {e.printstacktrace ();   }
4, the effect as shown:

————————————————————————————————————————————————— – Java Architect Project Combat, high concurrency cluster distributed, large data high availability video tutorials, total 760G

Download Address:

https://item.taobao.com/item.htm?id=555888526201

01. High-level architect 42 Phases
02.Java Advanced System Training Architecture Course 148 hours
03.Java Senior Internet Architect Course
04.Java Internet Architecture Netty, Nio, Mina, etc.-Video tutorials
05.Java Advanced Architecture Design 2016 finishing-video tutorials
06. Architect Foundation, advanced film
07.Java Architect Required Linux Operation series courses
08.Java Advanced System Training Architecture Course 116 hours
+
Hadoop series tutorials, Java design patterns and data structures, Spring Cloud Micro service, Springboot Primer
—————————————————————————————————————————————————–

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.