簡體   English   中英

JSP-Javascript,基於選擇選項值更新表字段

[英]JSP - Javascript, Update table fields based on select option value

我已經在互聯網上搜索了2天,但找不到針對我的問題的任何特定解決方案。

由於我是新手,所以我不知道該怎么做。

我有2列的數據庫表。 材料代碼和說明。

在我的JSP頁面上,我有一張桌子。 第一列是帶有來自數據庫的物料代碼的選擇選項。

基於此選擇,匹配的描述必須出現在第二欄中。 這是我無法完成的部分,這是代碼。

 </head>
    <body>
        <%
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            String url = "jdbc:mysql://localhost:3306/instock";
            String dbusername = "root";
            String dbpassword = "pswd";
            Connection con = DriverManager.getConnection(url, dbusername, dbpassword);
            ResultSet rs = null;

            PreparedStatement ps = con.prepareStatement("select * from master_materials");

        %>    

        <input type="submit" value="Save" />

        <table id="myTable" border="1">   
            <th>Material</th>
            <th>Desciption</th>
            <th>Quantity</th>

            <%for (int row = 1; row <= 5; row++) { %>
            <tr id="rowToClone">                
                <%--   <%for (int col=1; col <= 5; col++) { %>    --%>
                <%rs = ps.executeQuery();%>
                <td> 
                    <select>
                        <% while (rs.next()) {%>
                        <option><%=rs.getString(1)%></option>
                        <%}%>    
                    </select>

                </td>
                <td><input type="text" name="Description" value="" readonly="readonly" />  </td> 
                <td>adsasd </td> 
                    <%--       <% } %> --%>
            </tr>
            <% }%>
        </table>

    </body>
    <input type="button" onclick="cloneRow()" value="Clone Row" />
</html>

<script type="text/javascript">
    function cloneRow() {
        var row = document.getElementById("rowToClone"); // find row to copy
        var table = document.getElementById("myTable"); // find table to append to
        var clone = row.cloneNode(true); // copy children too
        clone.id = "newID"; // change id or other attributes/contents
        table.appendChild(clone); // add new row to end of table
    }
</script>

起初,在jsp上編寫Java代碼並不好。 當您填充選擇時,可以選擇包括值作為描述。 您可以使用javascript Onchange函數來捕獲選定的交互。

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

現在yourFunction ,你可以得到被選中的項目和它的描述,你可以設置在第二列此值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM