【发布时间】: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