在选择另一个选择框时填充选择框
我有两个选择框,两个都应从数据库中填充.第二个选择框应基于第一个选择框中的所选选项进行填充.数据库连接成功,我能够填充第一个选择框.但是我不知道如何根据我用来填充第一个选择框的first.code来填充第二个选择框,
I have two select boxes.Both of them should be populated from database.Second select box should be populated based on the selected option in the first select box.Database connectivity is success and i am able to populate the first select box.But i dont know how to populate second select box based on the first.code i used to populate first select box is,
<select class="weekcombo">
<%
List list= new DataManager().getlist();
for(int i = 0; i < list.size(); i++) {
out.write("<option value=\""+ list.get(i)+ "\">"+ list.get(i));
}
%>
我不知道是否为此使用servlet或其他东西.
I dont know whether to use servlet or something else for this.
对于第一个选择框,您可以默认填充值,就像上面的代码中提到的那样:
For first select box you can populate values by default like you have mentioned in above code :
<select class="weekcombo" onchange="populateSecValues(this)">
<%
List list= new DataManager().getlist();
for(int i = 0; i < list.size(); i++) {
out.write("<option value=\""+ list.get(i)+ "\">"+ list.get(i));
}
%>
</select>
<select id="secBox" class="weekcombo">
</select>
Javascript: 在urlString中,您可以传递第一个选择框值,就像我在下面的代码段中传递的一样
Javascript : In urlString you can pass the first select box value like I have passed in below code snippet
function populateSecValues(obj){
// use here ajax call .. which will populate second box data
var firstBoxValue = obj.value;
var urlString ="your_action_url?firstBoxValue="+firstBoxValue ;
$.ajax({
type: "POST",
url: urlString ,
success: function(result) {
console.info("result"+result);
$("#secBox").html(result);
}
});
}
从服务器以
<option value ="secBoxValue">secBoxValue</option>