ASP.Net jQuery DropDownList更改事件

ASP.Net jQuery DropDownList更改事件,jquery,.net,drop-down-menu,asp-classic,Jquery,.net,Drop Down Menu,Asp Classic,我想用ASP.Net触发jQuery更改事件。 以下是ASP和jQuery代码的一部分: <asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="body"> <p> <asp:DropDownList ID="DropDownList1" runat="server"> </asp:DropDownList> <asp:Label ID="

我想用ASP.Net触发jQuery更改事件。 以下是ASP和jQuery代码的一部分:

<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="body">
<p>
    <asp:DropDownList ID="DropDownList1" runat="server">
    </asp:DropDownList>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</p>
<script type ="text/javascript">
    $('#DropDownList1').change(function() {
        alert( "Handler for .change() called." );
    });
</script>
</asp:Content>


$('#DropDownList1')。更改(函数(){ 警报(“调用了.change()的处理程序”); });
试图通过id#body#DropDownList1和#DropDownList1获取元素。
不幸的是,在这两种情况下都没有输出。

如果您在开发人员工具中检查dropdownlist元素,您会注意到ID实际上不是DropdownList1。Asp基于id属性生成id以防止重复,因此呈现方式不同。您不能依赖于复制它生成的一个,因为它可能使用不同的ClientID呈现它。您只需将CssClass设置为选择即可:

<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="body">
<p>
    <asp:DropDownList CssClass="DropdownList1" ID="DropDownList1" runat="server">
    </asp:DropDownList>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
 </p>
 <script type="text/javascript">
    $('.DropdownList1').change(function() {
        alert( "Handler for .change() called." );
    });
 </script>
 </asp:Content>


$('.DropdownList1').change(函数(){ 警报(“调用了.change()的处理程序”); });
您还可以基于ClientID(客户端上实际呈现的ID)选择元素,如下所示:

$(“#”)

您可以将在浏览器上生成的HTML放入下拉列表吗?-是错的,应该是,你的例子就是这样工作的。
$("#<%=DropdownList1.ClientID %>")