1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > Ajax下拉列表添加数据

Ajax下拉列表添加数据

时间:2020-08-18 04:54:16

相关推荐

Ajax下拉列表添加数据

web前端|js教程

Ajax,数据,添加

web前端-js教程 1. 前台jsp,新建一个下拉控件

保险网源码,vscode里如何查找代码,ubuntu内存增加,tomcat多个项目 端口,爬虫引擎下载,php 在线解密工具,厦门seo排名怎么样,网站评论日期 源代码,查wordpress模板lzw

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

公司 源码 复制,vscode怎么同时写代码,刷 ubuntu 盒子,tomcat没办法,防爬虫检测,php与html哪个好学,云阳智能化seo推广,网站背景小图,asp模板时间限制lzw

function loadType(){ $.get( getAllTypes.action,function(data){ var $sel = $("#seldvd"); // console.log(data); for(var i = 0;i<data.length;i++){ $item = $(""); //添加option $item.val(data[i].id); //添加option的value ,数据库中用id和type保存的数据 $item.html(data[i].type); //添加option数据 $sel.append($item); //将option添加进select } },json ); }

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

网络会议系统端源码,vscode的字体安装,pip ubuntu安装,监控tomcat是否启动,sqlite3使用说明,广州春节有哪些爬虫店不关门,php 判断奇数,知名seo推广前景,网站好看的图标代码,精仿viun多本小说站模板lzw

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

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

typeInfo.java

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 getAllTypes(){ArrayList typeList = new ArrayList();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; // } }

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。