简体   繁体   English

根据优先选择下拉列表填充其他选择下拉列表

[英]populating other select drop down based on first select drop down

I am using struts 1.2, I have a very general requirement that, I have two following dropdown box. 我使用的是struts 1.2,我有一个非常一般的要求,即我有两个以下的下拉框。 now i want when we select the category, it should populate the subcatoegory select box. 现在我想要当我们选择类别时,它应该填充子类别选择框。 i tried with javascript but no luck is working. 我尝试使用JavaScript,但没有运气。

please some one guide me. 请有人指导我。

I can do this by using html select. 我可以通过使用html select来做到这一点。

 <html:select property="cat" onchange="getSubcatValue(this.value);">    
<html:option value="0">Category</html:option>
<html:optionsCollection name="PostaddForm" property="categoryList"  label="catName" value="catID"/>
</html:select>



 <html:select property="subCat" >
<html:option value="0">Category</html:option>
<html:optionsCollection name="PostaddForm" property="subCategoryList"  label="label" value="value"/>
</html:select>

Why don't you try using jQuery, it would be something like this: 您为什么不尝试使用jQuery,它就像这样:

<select id="mainDropDown" onchange="getSubcatValue()">
     //Options for mainDropDown list
</select>

<select id="subDropDown"></select>

<script type="text/javascript>
    function getSubcatValue() 
    {
        var chosenValue = $("#mainDropDown").val();
        var options = $('#subDropDown').prop('options');
        options = [];
        if (chosenValue == "something") {
            for (var i = 0 ; i < 10 ; i++) {
                options[i] = new Option(i+1, i); // populate the subDropDown with 10 options: 1, 2 ,3, 4, etc... 
                //The 1st parameter is the text for the option, the 2nd parameter is the value of the option
            }
        }
    }
</script>

EDIT: Based on Anthony comment, I'd like to add that the for loop is just for demonstrating purpose to help you get an idea how to populate the options. 编辑:基于Anthony的评论,我想补充一点,for循环仅用于演示目的,以帮助您了解如何填充选项。 In real world, for example, in my own web application, I made an Ajax call to retrieve the options data in XML to populate the drop down list. 例如,在现实世界中,在我自己的Web应用程序中,我进行了Ajax调用,以XML格式检索选项数据以填充下拉列表。

Hope it helps! 希望能帮助到你!

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

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