简体   繁体   中英

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. now i want when we select the category, it should populate the subcatoegory select box. i tried with javascript but no luck is working.

please some one guide me.

I can do this by using 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:

<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. 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.

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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