国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > AJAX > 正文

Ajax動(dòng)態(tài)為下拉列表添加數(shù)據(jù)的實(shí)現(xiàn)方法

2024-09-01 08:27:27
字體:
供稿:網(wǎng)友

 1. 前臺(tái)jsp,新建一個(gè)下拉控件

<select id="seldvd" onChange="sel_onchange(this)"></select> 

2. js部分,建一個(gè)function方法,利用ajax,指向 'getAllTypes.action' 的servlet部分,獲取傳來的下拉列表的數(shù)據(jù),動(dòng)態(tài)填充

<span style="white-space:pre"> </span>function loadType(){ <span style="white-space:pre">   </span>$.get(  <span style="white-space:pre">  </span>    'getAllTypes.action', <span style="white-space:pre">   </span>  function(data){ <span style="white-space:pre">   </span>   var $sel = $("#seldvd"); <span style="white-space:pre">     </span> // console.log(data); <span style="white-space:pre">   </span>   for(var i = 0;i<data.length;i++){ <span style="white-space:pre">     </span> <span style="white-space:pre">  </span>$item = $("<option></option>"); //添加option <span style="white-space:pre">     </span> <span style="white-space:pre">  </span>$item.val(data[i].id); //添加option的value ,<span style="line-height: 1.5; white-space: pre-wrap; font-family: Arial, Helvetica, sans-serif;"><span style="font-size:10px;">數(shù)據(jù)庫中用id和type保存的數(shù)據(jù)</span></span> <span style="white-space:pre">     </span> <span style="white-space:pre">  </span>$item.html(data[i].type); //添加option數(shù)據(jù) <span style="white-space:pre">     </span> <span style="white-space:pre">  </span>$sel.append($item); //將option添加進(jìn)select  <span style="white-space:pre">  </span>     }  <span style="white-space:pre">  </span>    },'json'  <span style="white-space:pre">  </span>   ); <span style="white-space:pre"> </span>} 

3. 新建一個(gè)servlet頁面,用來向Ajax返回?cái)?shù)據(jù)

public void doGet(HttpServletRequest request, HttpServletResponse response)       throws ServletException, IOException {     request.setCharacterEncoding("utf-8");     ArrayList<typeInfo> typeList = new ArrayList<typeInfo>();     typeDao td = new typeDao();     typeList = td.getAllTypes();     JSONArray arr = new JSONArray(typeList);//這里導(dǎo)入需要轉(zhuǎn)json數(shù)據(jù)包     String jsString = arr.toString();     //響應(yīng)到客戶端         request.setCharacterEncoding("utf-8");     response.setContentType("text/plain;charset=utf-8");     response.getWriter().print(jsString); //返回下拉列表需要的json格式數(shù)據(jù)   } 

4. 那么問題來了,這個(gè)數(shù)據(jù)來源在哪啊?當(dāng)然在數(shù)據(jù)庫(MySQL)。所以先要寫一個(gè)方法讀取數(shù)據(jù)庫中的數(shù)據(jù)

<strong>typeInfo.java</strong> 
import java.io.Serializable; public class typeInfo implements Serializable {   private int id;   private String type;   public int getId() {     return id;   }   public void setId(int id) {     this.id = id;   }   public String getType() {     return type;   }   public void setType(String type) {     this.type = type;   }   public typeInfo(){   }   public typeInfo(int id, String type) {     this.id = id;     this.type = type;   } }             
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 新宁县| 富顺县| 伊通| 化德县| 平南县| 德令哈市| 敦煌市| 平陆县| 盐源县| 盱眙县| 明溪县| 佛坪县| 基隆市| 磴口县| 嵊州市| 江源县| 阜平县| 左权县| 黄平县| 芦溪县| 广昌县| 定兴县| 崇明县| 贞丰县| 汉沽区| 克什克腾旗| 巴塘县| 阿瓦提县| 莲花县| 罗源县| 东兰县| 唐海县| 黄骅市| 长顺县| 赤壁市| 左云县| 昌图县| 平乐县| 鲁甸县| 陇南市| 织金县|