jsp:未发布的ajax填充的dropbox的值

时间:2014-10-23 11:39:25

标签: ajax jsp servlets

我在检索用ajax填充的dropbox的值时遇到问题。相关参数&价值根本不存在,无论选择什么价值。 我已经搜索了几天但没有找到这个问题的好答案,所以感谢您的帮助。 这是jsp站点的代码:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<script language="javascript" type="text/javascript">  
      var xmlHttp

      function showSockets(str){
          if (typeof XMLHttpRequest != "undefined"){
            xmlHttp= new XMLHttpRequest();
          }
          else if (window.ActiveXObject){
            xmlHttp= new ActiveXObject("Microsoft.XMLHTTP");
          }
          if (xmlHttp==null){
            alert("Browser does not support XMLHTTP Request")
            return;
          } 
          var url="sockets_abstract.jsp";
          url +="?room=" +str;
          xmlHttp.onreadystatechange = socketChange;
          xmlHttp.open("GET", url, true);
          xmlHttp.send(null);
      }

      function socketChange(){   
          if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){   
            document.getElementById("socket").innerHTML=xmlHttp.responseText   
          }   
      }  
</script>  

<title>AJAX abstract</title>
</head>
<body>
<table align="center">
<thead>
<tr><td colspan="2" class="heading">Test ajax:</td></tr>
</thead>
<tbody>
<form action="show_parameters.jsp" method="post" name="mainForm">
<tr> 
<td>Location*:</td>
<td>
    <select name="location" size="1" onchange="showSockets(this.value)">
        <option selected value="room1">room1</option>
        <option value="room2">room2</option>
        <option value="room3">room3</option>
    </select>
</td>
</tr>
<tr> 
<td>Socket*:</td><td>
      <div id="socket">  
      <select name="room_socket" onchange="getSocket(this.value)">
        <option value='-1'></option>
      </select>  
      </div> 
</td>
</tr>
<tr><td colspan="2"><hr width="80%"></hr></td></tr>
<input type="hidden" name="insert" value="true"/>
<tr><td colspan="2" align="right"><input style="width:100%"  type="submit" value="Check Parameters" /></td></tr>
</form>
</tbody>
</table>
</body>
</html>

以下是jsp-servlet的代码:

<%
String room=request.getParameter("room");  
String buffer="<select name=\"netplane_socket\">\n"; 
    if (room.equals("room1")) {
        buffer = buffer + "<option value=\"rm1skt1\">room1socket1</option>\n";
        buffer = buffer + "<option value=\"rm1skt2\">room1socket2</option>\n";
        buffer = buffer + "<option value=\"rm1skt3\">room1socket3</option>\n";
    }
    else if (room.equals("room2")) {
        buffer = buffer + "<option value=\"rm2skt1\">room2socket1</option>\n";
        buffer = buffer + "<option value=\"rm2skt2\">room2socket2</option>\n";
        buffer = buffer + "<option value=\"rm2skt3\">room2socket3</option>\n";
    }
    else if (room.equals("room3")) {
        buffer = buffer + "<option value=\"rm3skt1\">room3socket1</option>\n";
        buffer = buffer + "<option value=\"rm3skt2\">room3socket2</option>\n";
        buffer = buffer + "<option value=\"rm3skt3\">room3socket3</option>\n";
    }
    else {
        buffer = buffer + "<option value=\"novalidroom\">no valid room</option>\n";
    }
    buffer=buffer+"</select>";  
    response.getWriter().println(buffer); 
 %>

显示参数和值的代码:

<%@page import="java.util.*" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
   "http://www.w3.org/TR/html4/loose.dtd"> 

<html> 
    <head> 
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
        <title>JSP display all parameters</title> 
    </head> 
    <body> 
        <% 
            Enumeration en = request.getParameterNames();
            while (en.hasMoreElements()) {
                String parameterName = (String) en.nextElement();
                String parameterValue = request.getParameter(parameterName);
                out.println(parameterName+":"+parameterValue+"<br />");
            }
        %> 
    </body> 
</html> 

以前文件的结果:

location:room3
insert:true

因此完全忽略了socket dropbox。 谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

form标记不是tbody标记的有效子标记。如果您将表单标记移到表格外,它可以正常工作。

<form action="show_parameters.jsp" method="post" name="mainForm">
<table align="center">
<thead>
<tr><td colspan="2" class="heading">Test ajax:</td></tr>
</thead>
<tbody>

<tr> 
<td>Location*:</td>
<td>
    <select name="location" size="1" onchange="showSockets(this.value)">
        <option selected value="room1">room1</option>
        <option value="room2">room2</option>
        <option value="room3">room3</option>
    </select>
</td>
</tr>
<tr> 
<td>Socket*:</td><td>
      <div id="socket">  
      <select name="room_socket" onchange="getSocket(this.value)">
        <option value='-1'></option>
      </select>  
      </div> 
</td>
</tr>
<tr><td colspan="2"><hr width="80%"></hr></td></tr>
<input type="hidden" name="insert" value="true"/>
<tr><td colspan="2" align="right"><input style="width:100%"  type="submit" value="Check Parameters" /></td></tr>

</tbody>
</table>
</form>