<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
//写法1
//var ddl = document.getElementById("<%=ddlDept.ClientID %>");
//写法2
//var ddl=$("#<%=ddlDept.ClientID%>");
alert(ddl.selectedIndex);
}); function show(obj) {
var v = $(obj).val();
alert(v);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList runat="server" ID="ddlDept" onchange="show(this)" />
</div>
</form>
</body>
</html>
上面是前台代码,下面是后台代码.
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
List<string> list=new List<string>(){"aaa","bbb","ccc"};
ddlDept.DataSource = list;
ddlDept.DataBind();
ddlDept.SelectedIndex = ;
}
}
不知道用jQuery怎么写。
另一种版本的写法(全部在前台完成):
<head runat="server">
<title></title>
<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);
alert(Text);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<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>
</div>
</form>
</body>