在选择另一个选择框时填充选择框 [英] populate select box on selecting another selectbox

查看:62
本文介绍了在选择另一个选择框时填充选择框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个选择框,两个都应从数据库中填充.第二个选择框应基于第一个选择框中的所选选项进行填充.数据库连接成功,我能够填充第一个选择框.但是我不知道如何根据我用来填充第一个选择框的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>

这篇关于在选择另一个选择框时填充选择框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆