Ajax动态为下拉列表添加数据的实现方法

前端之家收集整理的这篇文章主要介绍了Ajax动态为下拉列表添加数据的实现方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编两巴掌来看看吧!
 

 1. 前台jsp,新建一个下拉控件

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

2. js部分,建一个function方法,利用ajax,指向 'getAllTypes.action' 的servlet部分,获取传来的下拉列表的数据,动态填充

  1. <span style="white-space:pre"> </span>function loadType(){
  2. <span style="white-space:pre"> </span>$.get(
  3. <span style="white-space:pre"> </span> 'getAllTypes.action',<span style="white-space:pre"> </span> function(data){
  4. <span style="white-space:pre"> </span> var $sel = $("#seldvd");
  5. <span style="white-space:pre"> </span> // console.log(data);
  6. <span style="white-space:pre"> </span> for(var i = 0;i<data.length;i++){
  7. <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>$item = $("<option></option>"); //添加option
  8. <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;">数据库中用id和type保存的数据</span></span>
  9. <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>$item.html(data[i].type); //添加option数据
  10. <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>$sel.append($item); //将option添加进select
  11. <span style="white-space:pre"> </span> }
  12. <span style="white-space:pre"> </span> },'json'
  13. <span style="white-space:pre"> </span> );
  14. <span style="white-space:pre"> </span>}

3. 新建一个servlet页面,用来向Ajax返回数据

  1. public void doGet(HttpServletRequest request,HttpServletResponse response)
  2. throws ServletException,IOException {
  3. request.setCharacterEncoding("utf-8");
  4. ArrayList<typeInfo> typeList = new ArrayList<typeInfo>();
  5. typeDao td = new typeDao();
  6. typeList = td.getAllTypes();
  7. JSONArray arr = new JSONArray(typeList);//这里导入需要转json数据包
  8. String jsString = arr.toString();
  9. //响应到客户端
  10. request.setCharacterEncoding("utf-8");
  11. response.setContentType("text/plain;charset=utf-8");
  12. response.getWriter().print(jsString); //返回下拉列表需要的json格式数据
  13. }

4. 那么问题来了,这个数据来源在哪啊?当然在数据库MysqL)。所以先要写一个方法读取数据库中的数据

  1. <strong>typeInfo.java</strong>
  1. import java.io.Serializable;
  2. public class typeInfo implements Serializable {
  3. private int id;
  4. private String type;
  5. public int getId() {
  6. return id;
  7. }
  8. public void setId(int id) {
  9. this.id = id;
  10. }
  11. public String getType() {
  12. return type;
  13. }
  14. public void setType(String type) {
  15. this.type = type;
  16. }
  17. public typeInfo(){
  18. }
  19. public typeInfo(int id,String type) {
  20. this.id = id;
  21. this.type = type;
  22. }
  23. }

TypeDao.java  (需要导入JDBC包)

  1. import java.sql.Connection;
  2. import java.sql.PreparedStatement;
  3. import java.sql.ResultSet;
  4. import java.util.ArrayList;
  5. import model.typeInfo;
  6. public class typeDao extends baseDao {
  7. public ArrayList<typeInfo> getAllTypes(){
  8. ArrayList<typeInfo> typeList = new ArrayList<typeInfo>();
  9. Connection con = null;
  10. PreparedStatement psm = null;
  11. ResultSet rs = null;
  12. try {
  13. con = super.getConnection();
  14. psm = con.prepareStatement("select * from types");
  15. rs = psm.executeQuery();
  16. while(rs.next()){
  17. typeInfo types = new typeInfo();
  18. types.setId(rs.getInt(1));
  19. types.setType(rs.getString(2));
  20. typeList.add(types);
  21. }
  22. } catch (Exception e) {
  23. System.out.println("显示所有类型报错:"+e.getMessage());
  24. }finally{
  25. super.closeAll(rs,psm,con);
  26. }
  27. return typeList;
  28. //
  29. }
  30. }

4. 好了,利用Tomcat ,现在打开网页,下拉列表就能显示数据了

原文链接:http://blog.csdn.net/chauncywu/article/details/54670716

(编程之家 jb51.cc jb51.cc)

猜你在找的Ajax相关文章