[英]populating other select drop down based on first select drop down
我使用的是struts 1.2,我有一個非常一般的要求,即我有兩個以下的下拉框。 現在我想要當我們選擇類別時,它應該填充子類別選擇框。 我嘗試使用JavaScript,但沒有運氣。
請有人指導我。
我可以通過使用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>
您為什么不嘗試使用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>
編輯:基於Anthony的評論,我想補充一點,for循環僅用於演示目的,以幫助您了解如何填充選項。 例如,在現實世界中,在我自己的Web應用程序中,我進行了Ajax調用,以XML格式檢索選項數據以填充下拉列表。
希望能幫助到你!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.