以下代码将仅在文本框中显示一个值,但我需要的是,如果我选择一个下拉选项,它必须在每个单独的文本框中显示其所有行值..在单独的文本框中检索两个或多个值数据库使用下拉 onchange 事件 jsp,而不使用表索引。
<%@ 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>
最佳答案
简单地将数据传递给元素的数据属性,如下所示
<option value="<%=rs.getString(2) %>" data-name="<%=rs.getString(3)%>" data-xyz="value-which-you-want-to-get-in-change-event"><%=rs.getString(1) %></option>
现在您可以在更改事件函数中获取该值
function populateCustomerId(){
var selectBox = document.getElementById('selectBox');
var selectedCustomerId = selectBox.options[selectBox.selectedIndex].value;
var xyz = selectBox.options[selectBox.selectedIndex].getAttribute('data-xyz');
var name = selectBox.options[selectBox.selectedIndex].getAttribute('data-name');
document.getElementById('customerId').value = selectedCustomerId;
document.getElementById('customerName').value = name;
document.getElementById('customerXyz').value = xyz;
}
这只是一个简单的示例,您可以如何做到这一点。 您可以根据需要对其进行改进。
关于javascript - 使用下拉 onchange 事件 jsp 在不使用表索引的情况下从数据库中检索单独文本框中的两个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51496216/