我有两个html下拉列表,使用jsp从数据库中检索它们的值。
<%
String query =" SELECT question_text,question_id FROM questions WHERE id = ?";
PreparedStatement stmt = conn.prepareStatement(query);
stmt.setString(1,request.getParameter("QID"));
ResultSet rs = stmt.executeQuery();
if(!rs.next()) {} else {%>
<form action="#" method="post">
<p> First Question </p>
<select name="currentQuestion">
<%do{%>
<option value="<%=rs.getString("question_id")%>"><%=rs.getString("question_text")%> </option>
<%}while(rs.next());}%>
</select>
<%
String query =" SELECT question_text,question_id FROM questions WHERE id = ? AND question id != ? ";
PreparedStatement stmt = conn.prepareStatement(query);
stmt.setString(1,request.getParameter("QID"));
stmt.setString(2,CHOOSEN QUESTION);
ResultSet rs = stmt.executeQuery();
if(!rs.next()) {} else {%>
<p> Next Question </p>
<select name="currentQuestion">
<%do{%>
<option value="<%=rs.getString("question_id")%>"><%=rs.getString("question_text")%></option>
<%}while(rs.next());}%>
</select>
</form>
现在,当用户从第一个下拉列表中选择特定问题时,第二个下拉列表的值不包括该问题? 谁知道怎么做?
答案 0 :(得分:1)
CHOOSEN QUESTION
。id
元素中的name
和option
个标记。CHOOSEN QUESTION
是变量的非法名称,因为它包含空格。onChange
上的第二个dropbox(在用户之前)选择一个选项 - 您可能希望禁用第二个保管箱)form
,它最终会将用户的选择提交给JSP(服务器端)。 UPDATE(使用JS更改选项的示例代码):
<html>
<head>
<script type="text/javascript" >
<!-- hide
function update(x){
if (x != "null") {
if (x == "1") {
var jumpmenu2 = document.getElementById("jumpmenu2");
var newOption1 = document.createElement('option');
newOption1.text = "a"; //HERE You'll use newOption1.text = "<?php echo $db_option_text;?>";
newOption1.value = "1"; //HERE You'll use newOption1.text = "<?php echo $db_option_value;?>";
var newOption2 = document.createElement('option');
newOption2.text = "b"; // same like above
newOption2.value = "2"; // same like above
var newOption3 = document.createElement('option');
newOption3.text = "c"; // same like above
newOption3.value = "3"; // same like above
jumpmenu2.remove(jumpmenu2.length-1);
jumpmenu2.remove(jumpmenu2.length-1);
jumpmenu2.remove(jumpmenu2.length-1);
try {
// For standard browsers
jumpmenu2.add(newOption1,null);
jumpmenu2.add(newOption2,null);
jumpmenu2.add(newOption3,null);
}
catch (ex) {
// For Microsoft Internet Explorer and other non-standard browsers.
jumpmenu2.add(newOption1);
jumpmenu2.add(newOption2);
jumpmenu2.add(newOption3);
}
}
else if (x == "2"){
var jumpmenu2 = document.getElementById("jumpmenu2");
var newOption1 = document.createElement('option');
newOption1.text = "d";
newOption1.value = "1";
var newOption2 = document.createElement('option');
newOption2.text = "e";
newOption2.value = "2";
var newOption3 = document.createElement('option');
newOption3.text = "f";
newOption3.value = "3";
jumpmenu2.remove(jumpmenu2.length-1);
jumpmenu2.remove(jumpmenu2.length-1);
jumpmenu2.remove(jumpmenu2.length-1);
try {
// For standard browsers
jumpmenu2.add(newOption1,null);
jumpmenu2.add(newOption2,null);
jumpmenu2.add(newOption3,null);
}
catch (ex) {
// For Microsoft Internet Explorer and other non-standard browsers.
jumpmenu2.add(newOption1);
jumpmenu2.add(newOption2);
jumpmenu2.add(newOption3);
}
}
}
}
// end hide -->
</script>
</head>
<body>
<form name="form1" id="form1">
<select name="jumpmenu" name="jumpmenu" onChange="update(document.form1.jumpmenu.options[document.form1.jumpmenu.options.selectedIndex].value)">
<option value=1>1</option>
<option value=2>2</option>
</select>
</form>
<select name="jumpmenu2" id="jumpmenu2">
<option value=a id=1>a</option>
<option value=b id=2>b</option>
<option value=c id=3>c</option>
</select>
</body>
</html>