JSP中下拉列表框的问题,求解决! 5
我想通过选择省份下拉框中相应的值,使城市下拉框中输出相应身份的城市下面是我SERVLET:publicvoiddoPost(HttpServletRequestreque...
我想通过选择省份下拉框中相应的值,使城市下拉框中输出相应身份的城市
下面是我SERVLET:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
String selected = request.getParameter("selected");
PrintWriter out = response.getWriter();
out.println("<response>");
if(selected.equals("2")){
out.println("<city>");
out.println("<cityname>长沙</cityname>");
out.println("<cityvalue>1</cityvalue>");
out.println("</city>");
out.println("<city>");
out.println("<cityname>益阳</cityname>");
out.println("<cityvalue>2</cityvalue>");
out.println("</city>");
out.println("<city>");
out.println("<cityname>湘潭</cityname>");
out.println("<cityvalue>3</cityvalue>");
out.println("</city>");
out.println("<city>");
out.println("<cityname>株洲</cityname>");
out.println("<cityvalue>4</cityvalue>");
out.println("</city>");
}else{
out.println("<city>");
out.println("<cityname>请选择城市</cityname>");
out.println("<cityvalue>1</cityvalue>");
out.println("</city>");
}
out.println("</response>");
out.flush();
out.close();
}
然后我是从XML文档读取城市到下拉框的一部分js:
var result;
if(xmlHttp.readyState == 4){
if(xmlHttp.status == 200){
result = xmlHttp.responseXML.getElementsByTagName("city");
while(document.all.cityid.options.length > 0){
document.all.cityid.removeChild(document.all.cityid.childNodes[0]);
}
for(var i = 0;i < result.length;i++){
var option = document.createElement("OPTION");
option.text = result[i].childNodes[0].childNodes[0].nodeValue;
option.value = result[i].childNodes[1].childNodes[0].nodeValue; document.all.cityid.options.add(option);
}
}
}
问题就在选择省份后城市下拉框是空白。是否是result得到的值是空的,还是编码不对,JSP显示不出来?我检查程序走到了上面那部分js,得到了object类型的result,然而后面就没消息了,城市下拉框为空白。IDE和Tomcat也没报错。我JSP上面内容:<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
。。。
<select id="provinceid" name="province" onChange="beginCreate()">
。。。
<select id="cityid" name="city">
。。。
小弟不才,弄不清楚,求大虾们帮我!不甚感激! 展开
下面是我SERVLET:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
String selected = request.getParameter("selected");
PrintWriter out = response.getWriter();
out.println("<response>");
if(selected.equals("2")){
out.println("<city>");
out.println("<cityname>长沙</cityname>");
out.println("<cityvalue>1</cityvalue>");
out.println("</city>");
out.println("<city>");
out.println("<cityname>益阳</cityname>");
out.println("<cityvalue>2</cityvalue>");
out.println("</city>");
out.println("<city>");
out.println("<cityname>湘潭</cityname>");
out.println("<cityvalue>3</cityvalue>");
out.println("</city>");
out.println("<city>");
out.println("<cityname>株洲</cityname>");
out.println("<cityvalue>4</cityvalue>");
out.println("</city>");
}else{
out.println("<city>");
out.println("<cityname>请选择城市</cityname>");
out.println("<cityvalue>1</cityvalue>");
out.println("</city>");
}
out.println("</response>");
out.flush();
out.close();
}
然后我是从XML文档读取城市到下拉框的一部分js:
var result;
if(xmlHttp.readyState == 4){
if(xmlHttp.status == 200){
result = xmlHttp.responseXML.getElementsByTagName("city");
while(document.all.cityid.options.length > 0){
document.all.cityid.removeChild(document.all.cityid.childNodes[0]);
}
for(var i = 0;i < result.length;i++){
var option = document.createElement("OPTION");
option.text = result[i].childNodes[0].childNodes[0].nodeValue;
option.value = result[i].childNodes[1].childNodes[0].nodeValue; document.all.cityid.options.add(option);
}
}
}
问题就在选择省份后城市下拉框是空白。是否是result得到的值是空的,还是编码不对,JSP显示不出来?我检查程序走到了上面那部分js,得到了object类型的result,然而后面就没消息了,城市下拉框为空白。IDE和Tomcat也没报错。我JSP上面内容:<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
。。。
<select id="provinceid" name="province" onChange="beginCreate()">
。。。
<select id="cityid" name="city">
。。。
小弟不才,弄不清楚,求大虾们帮我!不甚感激! 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询