繁体   English   中英

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

[英]populate select box based on the another 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>

如何根据第一个选择框的值填充第二个选择框

您可以在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.
   }

})

注意:这仅供参考。 您可以将两种方法的实现合而为一,也可以有多种方法。 我留给你决定。

编辑:您无法在同一个php页面中为您的第二个select标记生成选项(例如在您的问题中),因为在您的页面加载和用户为first select标记选择一个值时,您的php代码已经执行。

暂无
暂无

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

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