根据另一个选择框填充选择框 [英] populate select box based on the another select box

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

问题描述

有两个选择框,它们都是从数据库中填充的.第二个选择框应基于从第一个选择框中选择的值进行填充.第一个选择框已被填充,但我无法填充第二个选择框

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

推荐答案

您可以在javascript中使用一个函数来填充第二个select标签的选项.

You can have a function in javascript to populate options for your second select tag.

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.

您无法在同一php页面中为您的第二个select标签生成选项(就像您的问题一样),因为在您的页面加载和用户为first select标签选择一个值时,您的php代码已经执行.

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.

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

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