【问题标题】:Display values in textbox based dropdown selection jsp在基于文本框的下拉选择jsp中显示值
【发布时间】:2018-12-29 19:16:45
【问题描述】:

我想根据 DropDown Selection 从数据库中检索数据。以下代码正在工作,但使用 selectBox.options[selectBox.selectedIndex].value 仅检索一个值但我的数据库表中有三列。我想根据下拉列表中的选择来检索文本字段中的所有内容。

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@page import = "java.sql.*" %>
    <%@page import = "java.sql.DriverManager.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript">
function populateCustomerId(){
    var selectBox = document.getElementById('selectBox');
     var selectedCustomerId = selectBox.options[selectBox.selectedIndex].value;
    document.getElementById('customerId').value = selectedCustomerId;

}</script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Select</title>
</head>
<body>
<%
try{   
    Class.forName("com.mysql.jdbc.Driver");
    Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/ravi","root","root");
    Statement st = con.createStatement();
    ResultSet rs = null;
        rs = st.executeQuery("select rid, rname, rmbl from r"); 
%>
    <select id="selectBox" onchange="populateCustomerId();">
        <%while(rs.next()){ %>

            <option value="<%=rs.getString(2) %>"><%=rs.getString(1) %></option>

        <%} %>
        <%}
    catch (Exception e){
            e.printStackTrace();
        } %>

    </select>
    <input id="customerId" type="text" value="" />
    <input id="" type="text" value="" />
    <input id="" type="text" value="" />
</td>
</body>
</html>

【问题讨论】:

    标签: javascript mysql jsp servlets


    【解决方案1】:

    在你的代码中

    <select id="selectBox" onchange="populateCustomerId();">
            <%while(rs.next()){ %>
    
                <option value="<%=rs.getString(2) %>"><%=rs.getString(1) %></option>
    
            <%} %>
            <%}
        catch (Exception e){
                e.printStackTrace();
            } %>
    
        </select>
    

    对于结果集中的每一行 (while(rs.next())
    您添加一个选项,其值是结果集中第二个字段的字符串 (rs.getString(2))
    以及结果集中第一个字段的显示值 (rs.getString(1))

    如果显示有问题,那就换个

    <%=rs.getString(1) %>  
    

    与

    rid: <%=rs.getString("rid") %> rname: <%=rs.getString("rname") %> rmbl: <%=rs.getString("rmbl") %>
    

    【讨论】:

    • 它在一个文本框中显示所有值。我需要在不同的文本框中显示每个值谢谢
    • 当我们将 替换为 rid: rname: rmbl:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多