Share a small instance, speak nonsense, and directly upload the code .....
Paste the JS Code:
JS Code 1 <SCRIPT type = "text/JavaScript">
2 $ (function (){
3 $ ("# btnget"). Click (function (){
4 $. Ajax ({
5 URL: "getdatas. ashx ",
6 type: "Post ",
7 contenttype: "application/JSON ",
8 datatype: "JSON ",
9 success: function (data ){
10 var DDL =$ ("# ddldatas ");
11
12 // delete a node
13 removeoption ();
14
15 // Method 1: Add a default Node
16 DDL. append ("<option value = '-1'> -- select -- </option> ");
17
18 // Method 2: Add a default Node
19 // DDL [0]. Options. Add (New Option ("-- select --", "-1 "));
20
21 // convert to a JSON object
22 var result = eval (data );
23
24 // cyclically traverse the drop-down box Binding
25 $ (result). Each (function (key ){
26 // method 1
27 var opt = $ ("<option> </option>"). Text (result [Key]. proname). Val (result [Key]. proid );
28 DDL. append (OPT );
29
30 // method 2
31 // var proid = Result [Key]. proid;
32 // var proname = Result [Key]. proname;
33 // call a custom Method
34 // appendoption (proid, proname );
35 });
36
37 // method 3
38 // $. Each (result, function (Key, value ){
39 // alert ("DD ");
40 // var op = New Option (value. proname, value. proid );
41 // DDL [0]. Options. Add (OP );
42 //});
43 },
44 error: function (data ){
45 alert ("error ");
46}
47 });
48 });
49
50
51 });
52
53 function removeoption (){
54 $ ("# ddldatas option"). Remove ();
55}
56
57 function appendoption (value, text ){
58 $ ("# ddldatas"). append ("<option value = '" + value + "'>" + TEXT + "</option> ");
59}
60 </SCRIPT>
Paste HTML code:
Html1 <body>
2 <Form ID = "form1" runat = "server">
3 <div>
4 <asp: dropdownlist id = "ddldatas" name = "prov" runat = "server" style = "width: 200px;">
5 </ASP: dropdownlist>
6 <input type = "button" id = "btnget" value = "Get Data"/>
7 </div>
8 </form>
9 </body>
Paste the background ashx page to obtain the data code:
Get data 1 public class getdatas: ihttphandler
2 {
3 // simple data acquisition for test
4 Public void processrequest (httpcontext context)
5 {
6 string sqlconn = @ "Data Source = admin-PC; initial catalog = holyknight; persist Security info = true; user id = ni; Password = ******";
7 string SQL = "select * From Provice ";
8 using (sqlconnection conn = new sqlconnection ())
9 {
10 dataset DS = BAF. database. sqlhelper. executedataset (sqlconn, commandtype. Text, SQL );
11 datatable dt = Ds. Tables [0];
12 // convert to JSON format data
13 string prostr = jsonconvert. serializeobject (DT );
14 context. response. contenttype = "text/plain ";
15 context. response. Write (prostr );
16
17}
18}
Effect:
Obtain the selected value:
Value 1 // Value
2 $ ("# ddldatas"). Change (function (){
3 // obtain the selected text value
4 var selecttext = $ (this). Find ("option: Selected"). Text ();
5 alert (selecttext );
6 // obtain the selected value
7 var selectvlaue = $ (this). Find ("option: Selected"). Val ();
8 alert (selectvlaue );
9 });