Jquery .on不能处理动态内容

jquery .on is not working on dynamic content

本文关键字:动态 处理 on 不能 Jquery      更新时间:2023-09-26

我有一个有许多行的listview,当用户单击任何一行时,我加载关于该行的数据并将结果放在另一个div中。

这个动态内容有这样的代码:

<div id="reasondiv">
                    <div style="float: left;padding-left:10px;  margin-left:134px;">
                        <label style="color: red; padding-right: 3px;">*</label>
                        <asp:DropDownList runat="server" ID="callDispoSelect" ClientIDMode="Static">
                            <asp:ListItem Value="-1">Select Reason</asp:ListItem>
                            <asp:ListItem Value="1">Reservation</asp:ListItem>
                            <asp:ListItem Value="2">Change of Reservation</asp:ListItem>
                            <asp:ListItem Value="3">Cancellation</asp:ListItem>
                            <asp:ListItem Value="4">Wait List</asp:ListItem>
                            <asp:ListItem Value="5">Other</asp:ListItem>
                        </asp:DropDownList>
                    </div>
                    <div style="float:left">
                        <input runat="server" id="visitID" ClientIDMode="Static"/>
                        <label id="importantSign" style="color: red; padding-right: 3px">*</label>
                    </div>
                </div>

我有这个jquery代码:

$(document).on('change', '#callDispoSelect', function () {
    var selectedValue = $("#callDispoSelect").val();
    if ((selectedValue == 1) || (selectedValue == 5)) {
        $("#visitID").show();
        $("#importantSign").show();
        $("#saveandclosebutton").hide();
    } else {
        if (selectedValue == -1) {
            $("#visitID").hide();
            $("#importantSign").hide();
            $("#saveandclosebutton").hide();
        } else {
            $("#visitID").hide();
            $("#importantSign").hide();
            $("#saveandclosebutton").show();
        }
    }
});

我在on中得到了undifined is not a function

我通过谷歌读了很多,我发现我必须使用$(document),这就是我所做的。但这并没有帮助我

我可以看到id没有改变。我从谷歌F12和Firebug中看到,所以id没有改变

注意

我正在使用这个加载动态内容:

$('#subView').load('SubView.aspx');

请帮

你确定你使用的是一个体面的jQuery版本吗?当我遇到这样的问题时,几乎都是旧版本的问题。

From http://api.jquery.com/on/;

  $(function () {
        $('#callDispoSelect').on('change', function () {
            var selectedValue = $('#callDispoSelect').val();
        if ((selectedValue == 1) || (selectedValue == 5)) {
            $("#visitID").show();
            $("#importantSign").show();
            $("#saveandclosebutton").hide();
        } else {
            if (selectedValue == -1) {
                $("#visitID").hide();
                $("#importantSign").hide();
                $("#saveandclosebutton").hide();
            } else {
                $("#visitID").hide();
                $("#importantSign").hide();
                $("#saveandclosebutton").show();
            }
        }
        });
    });