选择下拉列表后显示标签

时间:2015-05-22 09:14:39

标签: mysql jsp select drop-down-menu

我在mysql数据库中有2列,名称和值。这些列有多行。我需要在jsp页面的下拉列表中显示名称,并根据选择的名称,其值应显示在下面。 请提供一些代码。

我的代码: -

<%
  try{
           conn=  ConnectionHelper.getConnection();
           pstmt=conn.prepareStatement("select name, value from myDb ");
           rs=pstmt.executeQuery();
           %>
    <select id="selectDropDown" name="filename" >
        <%  while(rs.next()){  %>
            <option ><%= rs.getString("name")%> </option>
        <% } %>
        </select>
    value :<%= rs.getString("value")%>
    <%
                    } catch (SQLException sq) {
                        out.println("OOps! Something went wrong. DB error!");
                        sq.printStackTrace();
                    } catch (Exception e) {
                        out.println("OOps! Something went wrong.");
                        e.printStackTrace();
                    } finally {
                    }
        %>

1 个答案:

答案 0 :(得分:1)

我不确定你是如何编码的,但这就是我编码的方式。也许你可以按照这种方式去做。使用两个jsp文件

JSP

<%
  Vector vName = new Vector();

  String SQL = "SELECT NAME FROM TB_TEST";
  DB_TEST.makeConnection();
  DB_TEST.executeQuery(SQL);
  while(DB_TEST.getNextQuery())
  {
           String sNAME    = DB_TEST.getColumnString("NAME");

          vName.addElement(sNAME);
  }
  DB_TEST.takeDown();
%>

javascript部分

function fnTest()
{
   var test_value = document.mainform.TEST_NAME.value;
   document.mainform.action = "pop_test2.jsp?value="+test_value;
   document.mainform.submit();
}

HTML

<select name="TEST_NAME" onchange="fnTest();">
<option value="">--- Please Select ---</option>
<%
    for (int i=0;i<vNAME.size();i++) {
          String sNAME= (String) vNAME.elementAt(i);
 %>      
    <option value="<%=sNAME%>" ><%= sNAME%></option>
<%}%>  
</select>
<input type="text" name="TEST_VALUE" value="">

从下拉列表中选择名称后,它将重定向到pop_test2.jsp

pop_test2.jsp

<%
  String sNAME  = request.getParameter("value");
  String sVALUE = "";

  String SQL = "SELECT VALUE FROM TB_TEST WHERE NAME='"+sNAME+"' ";
  DB_TEST.makeConnection();
  DB_TEST.executeQuery(SQL);
  while(DB_TEST.getNextQuery())
  {
           sVALUE   = DB_TEST.getColumnString("VALUE");
  }
  DB_TEST.takeDown();
%>
<script language="Javascript">
    parent.document.mainform.TEST_VALUE.value = "<%=sVALUE%>";
</script>