简体   繁体   English

根据另一个选择框填充选择框

[英]populate select box based on the another select box

There are two select boxes ,both of them are populating from database.second select box should be populate based on the value selected from the first select box.first select box is already populated but i am unable to populate the second select box 有两个选择框,它们都是从数据库中填充的。第二个选择框应基于从第一个选择框中选择的值进行填充。第一个选择框已被填充,但是我无法填充第二个选择框

<select id="country_obj" name="custCountry" class="field_size_e">

    <%
                                    Iterator contryIter = countries.iterator();
                                    Lookup lookup = null;
                                    while(contryIter.hasNext()) {
                                        lookup = (Lookup)contryIter.next();
                                        if(bbForm.getCircuit().getCustCountry().equalsIgnoreCase(lookup.getLabel())){
                                            out.print("<option selected=\"selected\" value='"+lookup.getValue()+"'");
                                            out.print(">");
                                            out.print(lookup.getLabel());
                                            out.println("</option>");
                                        }else{
                                        out.print("<option value='"+lookup.getValue()+"'");
                                        out.print(">");
                                        out.print(lookup.getLabel());
                                        out.println("</option>");
                                        }
                                    }
                                %>
    </select>

how do i populate the second select box based on the value of first select box 如何根据第一个选择框的值填充第二个选择框

You can have a function in javascript to populate options for your second select tag. 您可以在javascript中使用一个函数来填充第二个select标签的选项。

function populate(val)
{
   //val is a string similar to "<option value='new_option'>new option</option><option...</option>..."
   $('#my_select2').append(val);
}

//below method is triggered whenever you select a value for your first select box
$("#country_obj").change(function(){
   var selected_str = $("#country_obj option:selected").text();
   //now pass this selected_str to a php page where options generation method is          present using $.ajax (refer http://api.jquery.com/jQuery.ajax/)
   //or you can implement the method here (in javascript) to generate options for your new select tag.
   }

})

Note: This is just for guidance. 注意:这仅供参考。 You can have implementations of both methods in one or you can have multiple methods. 您可以将两种方法的实现合而为一,也可以有多种方法。 I'll leave that for you to decide. 我留给你决定。

EDIT: you can't have options generated for your second select tag in the same php page (like in your question) because, by the time your page is loaded and a user selects a value for first select tag your php code is already executed. 编辑:您无法在同一个php页面中为您的第二个select标记生成选项(例如在您的问题中),因为在您的页面加载和用户为first select标记选择一个值时,您的php代码已经执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM