JavaScript为DropDownList添加新项
<script type="text/javascript" language="javascript">
function SearchChange()
{
var ddl = document.getElementById("DropDownList1")
var index = ddl.selectedIndex;
var Value = ddl.options[index].value;
var Text = ddl.options[index].text;
alert(Value);
}
</script>
调用:
<asp:DropDownList ID="DropDownList1" runat="server" onchange="SearchChange();">
<asp:ListItem Value="0">111</asp:ListItem>
<asp:ListItem Value="1">222</asp:ListItem>
<asp:ListItem Value="2">333</asp:ListItem>
</asp:DropDownList>
动态为dropdownlist添加项
<script type="text/javascript" >
function ChangeCategory() {
var dropAppType = document.getElementById("<%=dropAppType.ClientID%>");
var dropCategory = document.getElementById("<%=dropCategory.ClientID%>");
var index = dropAppType.selectedIndex;
if (dropAppType.options[index].text == "Application") {
dropCategory.options.length = 0;
dropCategory.options[0] = new Option("Comics", "0");
dropCategory.options[1] = new Option("Communication", "1");
dropCategory.options[2] = new Option("entertainment", "2");
dropCategory.options[3] = new Option("Finance", "3");
dropCategory.options[4] = new Option("Health", "4");
dropCategory.options[5] = new Option("Lifestyle", "5");
}
else if (dropAppType.options[index].text == "Games") {
dropCategory.options.length =0;
dropCategory.options[0]=new Option("arcade&action","0");
dropCategory.options[1]=new Option("brain@puzzle","1");
dropCategory.options[2]=new Option("cards@casino","2");
dropCategory.options[3] = new Option("casual", "3");
}
var hidCategory = document.getElementById("hidCategory");
hidCategory.value = dropCategory.options[index].text;
}
</script>