Ajax動態(tài)為下拉列表添加數(shù)據(jù)的實現(xiàn)方法
1. 前臺jsp,新建一個下拉控件
<select id="seldvd" onChange="sel_onchange(this)"></select>
2. js部分,建一個function方法,利用ajax,指向 'getAllTypes.action' 的servlet部分,獲取傳來的下拉列表的數(shù)據(jù),動態(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添加進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. 新建一個servlet頁面,用來向Ajax返回數(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);//這里導入需要轉(zhuǎn)json數(shù)據(jù)包 String jsString = arr.toString(); //響應到客戶端 request.setCharacterEncoding("utf-8"); response.setContentType("text/plain;charset=utf-8"); response.getWriter().print(jsString); //返回下拉列表需要的json格式數(shù)據(jù) }
4. 那么問題來了,這個數(shù)據(jù)來源在哪啊?當然在數(shù)據(jù)庫(MySQL)。所以先要寫一個方法讀取數(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; } }
TypeDao.java (需要導入JDBC包)
import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import model.typeInfo; public class typeDao extends baseDao { public ArrayList<typeInfo> getAllTypes(){ ArrayList<typeInfo> typeList = new ArrayList<typeInfo>(); Connection con = null; PreparedStatement psm = null; ResultSet rs = null; try { con = super.getConnection(); psm = con.prepareStatement("select * from types"); rs = psm.executeQuery(); while(rs.next()){ typeInfo types = new typeInfo(); types.setId(rs.getInt(1)); types.setType(rs.getString(2)); typeList.add(types); } } catch (Exception e) { System.out.println("顯示所有類型報錯:"+e.getMessage()); }finally{ super.closeAll(rs, psm, con); } return typeList; // } }
4. 好了,利用Tomcat ,現(xiàn)在打開網(wǎng)頁,下拉列表就能顯示數(shù)據(jù)了
以上所述是小編給大家介紹的Ajax動態(tài)為下拉列表添加數(shù)據(jù)的實現(xiàn)方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關文章
AJAX實現(xiàn)文件上傳功能報錯Current request is not a&n
這篇文章主要介紹了AJAX文件上傳功能實現(xiàn)中出現(xiàn)報錯Current request is not a multipart request的問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-07-07Ajax 給 XMLHttpReq.onreadystatechange傳遞參數(shù)
這篇文章主要介紹了Ajax如何給XMLHttpReq.onreadystatechange =函數(shù)傳遞參數(shù),需要的朋友可以參考下2014-05-05asp+ajax實現(xiàn)靜態(tài)頁面分頁的代碼
asp+ajax實現(xiàn)靜態(tài)頁面分頁的代碼...2007-11-11使用Ajax、json實現(xiàn)京東購物車結(jié)算界面的數(shù)據(jù)交互實例
這篇文章主要介紹了使用Ajax、json實現(xiàn)京東購物車結(jié)算界面的數(shù)據(jù)交互實例,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-02-02JQuery ajax返回JSON時的處理方式 (三種方式)
json數(shù)據(jù)是一種經(jīng)型的實時數(shù)據(jù)交互的數(shù)據(jù)存儲方法,使用到最多的應該是ajax與json配合使用了,下面由腳本之家小編給大家分享JQuery ajax返回JSON時的處理方式 (三種方式),需要的朋友可以參考下2015-09-09