javascript - 使用下拉 onchange 事件 jsp 在不使用表索引的情况下从数据库中检索单独文本框中的两个值

标签 javascript mysql scriptlet

以下代码将仅在文本框中显示一个值,但我需要的是,如果我选择一个下拉选项,它必须在每个单独的文本框中显示其所有行值..在单独的文本框中检索两个或多个值数据库使用下拉 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/

相关文章:

javascript - react 状态没有 Jest 更新

javascript - 如何从 Webpack 的导出中排除 vendor 模块 peerDependencies?

javascript - 如何在 css 链接标签中插入 javascript 函数

java - 如何将 java 与 html 和 javascript 混合在一起?

javascript - Angular ng-show 指令不适用于按钮

javascript - "Refreshing"JQuery 日期选择器小部件

javascript - Javascript 上的 Mysql 查询

mysql - 在 SELECT [MYSQL] 中加入或选择

mysql - 没有子查询,但是mysql抛出子查询返回超过1行