这对于在输入框中获取与选择框中相同的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>");
}%&gt;`
答案 0 :(得分:0)
使用onChange()
为选择框调用此方法。
function filltxtbox(){
document.getElementById("textBoxId").value =
document.getElementById("selectBoxId").options[e.selectedIndex].value;
}
document.getElementById("selectBoxId").options[e.selectedIndex].text
这将给出所选的测试(即category_desc)