关于动态select问题和数据进行合并问题。有图有代码 求救~~~~~
这个问题原先问过一次可能是因为我没说的太明白所以也没找到答案……这次上图 上代码。
我想编写一个简单的材料管理系统,所以页面需要一个动态的select菜单(不仅仅要连接到数据库上)
这个图上可以看到有一个下拉菜单,因为是要运用到材料管理系统所以需要操作N个项目(N不确定),这样我就希望可以JSP能自动生成select菜单(自动生成的菜单内项目和原有项目一样,为的是再选择需要更改的项目数)我的代码如下:
<head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><title>Insert title here</title></head><body><jsp:useBean id="conn_idname" class="com.connection_idname" /><%String sql="select id_name from tab_name";ResultSet rs=conn_idname.executeQuery(sql);%><form action="result.jsp" method="post"><select name="id_name"><option value="">请选择<%try{ while(rs.next()){ // String id_name=rs.getString("id_name"); %> <option value="<%=rs.getString("id_name") %>"><%=rs.getString("id_name") %></option> <% }}catch(Exception e){}%></select>请输入订货量:<input type="text" name="num_text"><br><input type="submit" value="enter"></form></body></html>
}
function state_Change() {
var province = document.getElementById("province");
var country = document.getElementById("country");
if (xmlhttp.readyState == 4&&xmlhttp.status == 200) {// 4 = "loaded"
var serviceData = xmlhttp.responseText;
if(serviceData == null||serviceData == ""){
return;
}
var s = serviceData.split(",");
for(var i=0;i<s.length-1;i++){
country.options[i+1] = new Option(s[i],s[i]);
}
}
}
</script>
</head>
<body>
出生地:<select id="province" onchange="loadXMLDoc()">
<option value="">------请选择省-----</option>
<option value="江西省">江西省</option>
<option value="江苏省">江苏省</option>
<option value="浙江省">浙江省</option>
<option value="山东省">山东省</option>
<option value="辽宁省">辽宁省</option>
<option value="福建省">福建省</option>
</select>
<select id="country"><option value="">-----请选择市-----</option></select>
</body>
</html>
servlet:
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.neusoft.zjl.service.ProvinceService;
public class GetProvince extends HttpServlet {
/**
* Constructor of the object.
*/
public GetProvince() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
List list = null;
ProvinceService provinceService = new ProvinceService();
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String strprovince = request.getParameter("province");
if(strprovince == null||"".equals(strprovince)){
return ;
}
String province = new String(strprovince.getBytes("ISO-8859-1"),"UTF-8");
list = provinceService.getCountry(province);//这个是查询数据库的你可以换你的对数据库的操作
if(list ==null){
return;
}
StringBuffer sb = new StringBuffer();
for(int i=0;i<list.size();i++){
String s = (String)list.get(i);
sb.append(s+",");
}
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.print(sb);
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
out.print(sb)这行代码就把你从数据库中得到的数据返回到jsp里了
然后通过
var serviceData = xmlhttp.responseText;
//接收到后台的数据
if(serviceData == null||serviceData == ""){
return;
}
var s = serviceData.split(",");
for(var i=0;i<s.length-1;i++){
country.options[i+1] = new Option(s[i],s[i]);
}
//将数据填充到select中