开发者

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="cat开发者_JS百科ID"/>
</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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜