0

这是在输入框中获取与选择框中相同的 category_name 的 javacript,但我不知道获取相同的 category_id 和 category_desc 的代码

<script type="text/javascript"> function filltxtbox(){ var e = document.getElementById("sel"); var strUser = e.options[e.selectedIndex].text; document.getElementById("txtselname").value=strUser; document.getElementById(defcatid).value } </script>

这是我设置默认值的jsp页面代码,但我希望这些值随着选择选项值的变化而变化,如果您需要任何其他信息,请告诉我。

                <%! Connection con = null;%>
                <%! Statement st = null;%>
                <%! ResultSet rs = null;%> 
                <%
                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                    con = DriverManager.getConnection("jdbc:derby://localhost:1527/mydb1", "mydb", "mydb");
                    st = con.createStatement();
                    rs = st.executeQuery("SELECT category_id, category_name FROM Category");
                    con.setAutoCommit(false);
                %>
                <tr>
                    <td>
                        CATEGORY NAME
                    </td>
                    <td>
                        <%-- using select tag  --%>
                        <select id="sel" onchange="filltxtbox()">
                            <%
                                String catname = null;
                                int catid = 0;
                                while (rs.next()) {
                                    catname = rs.getString(2);
                                    catid = rs.getInt(1);
                                    out.println("<option value=" + catid + ">" + catname + "</option>");
                                }
                            %>
                        </select>
                    </td>
                    <jsp:setProperty name="category" property="*"/>
                    <%--// category_id input  --%>
                <tr>
                    <td>
                        CATEGORY ID
                    </td>
                    <td>
                        <%  rs = st.executeQuery("SELECT category_id FROM Category");
                            if (rs.next()) {
                                int defcatid = rs.getInt(1);
                                out.print("<input  type = 'text' name = 'category_id' id='txtselid' value ='" + defcatid + "' size = '30'/>  ");
                            }
                        %>
                    </td>
                </tr>

                <%--// category_name input  --%>
                <tr>
                    <td>
                        CATEGORY NAME
                    </td>
                    <td>
                        <%  rs = st.executeQuery("SELECT category_name FROM Category");
                            if (rs.next()) {
                                String defcatname = rs.getString(1);
                                out.print("<input  type = 'text' name = 'category_name' id='txtselname' value ='" + defcatname + "' size = '30'/>  ");
                            }
                        %></td>
                </tr>

                <%--// category_desc input  --%>
                <tr>
                    <td>
                        CATEGORY DESCRIPTION
                    </td>
                    <td><%
                        rs = st.executeQuery("SELECT category_desc FROM category");
                        if (rs.next()) {
                   String defcatdesc = rs.getString(1);
                   out.print("<textarea name='category_desc' rows='10' cols='50' >" +defcatdesc + "</textarea>");

}%>`

4

1 回答 1

0

onChange()使用选择框调用此方法。

function filltxtbox(){ 
   document.getElementById("textBoxId").value = 
   document.getElementById("selectBoxId").options[e.selectedIndex].value;
  }

document.getElementById("selectBoxId").options[e.selectedIndex].text 这将给出选定的测试(即 category_desc)

于 2013-05-24T12:31:29.623 回答