Ajax動(dòng)態(tài)為下拉列表添加數(shù)據(jù)的實(shí)現(xiàn)方法
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;
}
}
TypeDao.java (需要導(dǎo)入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("顯示所有類型報(bào)錯(cuò):"+e.getMessage());
}finally{
super.closeAll(rs, psm, con);
}
return typeList;
//
}
}
4. 好了,利用Tomcat ,現(xiàn)在打開網(wǎng)頁,下拉列表就能顯示數(shù)據(jù)了
以上所述是小編給大家介紹的Ajax動(dòng)態(tài)為下拉列表添加數(shù)據(jù)的實(shí)現(xiàn)方法,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
ajax JSONP請(qǐng)求處理回調(diào)函數(shù)jsonpCallback區(qū)分大小寫
使用ajax進(jìn)行 JSONP跨域請(qǐng)求,因?yàn)楸徽?qǐng)求的對(duì)方的回調(diào)函數(shù)名稱是無法修改,想到設(shè)置AJAX 的JSONP參數(shù)。但是發(fā)現(xiàn)根本不起作用。最后偶然發(fā)現(xiàn) jsonpcallback是區(qū)分大小寫的2013-09-09
AJAX+Servlet實(shí)現(xiàn)的數(shù)據(jù)處理顯示功能示例
這篇文章主要介紹了AJAX+Servlet實(shí)現(xiàn)的數(shù)據(jù)處理顯示功能,結(jié)合實(shí)例形式分析了前臺(tái)ajax與后臺(tái)Servlet生成隨機(jī)數(shù)顯示的相關(guān)交互操作技巧,需要的朋友可以參考下2018-06-06
Ajax實(shí)現(xiàn)文件上傳功能(Spring MVC)
這篇文章主要為大家詳細(xì)介紹了Ajax實(shí)現(xiàn)文件上傳功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02
Ajax實(shí)現(xiàn)的異步傳輸與驗(yàn)證示例代碼
Ajax異步傳輸應(yīng)用很廣當(dāng)用戶注冊(cè)時(shí),當(dāng)用戶剛一輸完,立即判斷用戶是否存在這就用到了異步傳輸2014-01-01
JQuery ajax中error返回錯(cuò)誤及一直返回error的解答
本文由腳本之家小編給大家分享有關(guān) JQuery ajax中error返回錯(cuò)誤及一直返回error的解答總結(jié),需要的朋友可以參考下2015-09-09
Ajax提交參數(shù)的值中帶有html標(biāo)簽不能提交成功的解決辦法(ASP.NET)
這篇文章主要介紹了Ajax提交參數(shù)的值中帶有html標(biāo)簽不能提交成功的解決辦法(ASP.NET),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2016-12-12
Ajax讀取XML實(shí)現(xiàn)動(dòng)態(tài)下拉導(dǎo)航
Ajax讀取XML實(shí)現(xiàn)動(dòng)態(tài)下拉導(dǎo)航...2007-02-02

