使用dropdown onchange event jsp从数据库的不同文本框中检索两个值,而不使用表索引

nxowjjhe  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(390)

下面的代码将只在文本框中显示一个值,但我需要的是,如果我选择一个下拉选项,它必须在每个单独的文本框中显示它的所有行值..使用dropdown onchange event jsp从数据库中检索单独文本框中的两个或多个值,而不使用表索引。

  1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  2. pageEncoding="ISO-8859-1"%>
  3. <%@page import = "java.sql.*" %>
  4. <%@page import = "java.sql.DriverManager.*" %>
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  6. <html>
  7. <head>
  8. <script type="text/javascript">
  9. function populateCustomerId(){
  10. var selectBox = document.getElementById('selectBox');
  11. var selectedCustomerId = selectBox.options[selectBox.selectedIndex].value;
  12. document.getElementById('customerId').value = selectedCustomerId;
  13. }</script>
  14. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  15. <title>Select</title>
  16. </head>
  17. <body>
  18. <%
  19. try{
  20. Class.forName("com.mysql.jdbc.Driver");
  21. Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/ravi","root","root");
  22. Statement st = con.createStatement();
  23. ResultSet rs = null;
  24. rs = st.executeQuery("select rid, rname, rmbl from r");
  25. %>
  26. <select id="selectBox" onchange="populateCustomerId();">
  27. <%while(rs.next()){ %>
  28. <option value="<%=rs.getString(2) %>"><%=rs.getString(1) %></option>
  29. <%} %>
  30. <%}
  31. catch (Exception e){
  32. e.printStackTrace();
  33. } %>
  34. </select>
  35. <input id="customerId" type="text" value="" />
  36. <input id="" type="text" value="" />
  37. <input id="" type="text" value="" />
  38. </td>
  39. </body>
  40. </html>
sqyvllje

sqyvllje1#

简单地将数据传递到元素的数据属性,如下所示

  1. <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>

现在可以在change事件函数中获得该值

  1. function populateCustomerId(){
  2. var selectBox = document.getElementById('selectBox');
  3. var selectedCustomerId = selectBox.options[selectBox.selectedIndex].value;
  4. var xyz = selectBox.options[selectBox.selectedIndex].getAttribute('data-xyz');
  5. var name = selectBox.options[selectBox.selectedIndex].getAttribute('data-name');
  6. document.getElementById('customerId').value = selectedCustomerId;
  7. document.getElementById('customerName').value = name;
  8. document.getElementById('customerXyz').value = xyz;
  9. }

这只是一个简单的例子。你可以根据需要改进它。

展开查看全部

相关问题