jquery .on没有处理动态内容

时间:2014-05-22 10:36:53

标签: javascript jquery asp.net

我有一个包含很多行的列表视图,当用户点击任意一行时,我会加载有关该行的数据并将结果放在另一个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();
        }
    }
});

我在undifined is not a function

中获得on

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

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

注释

我正在使用以下内容加载动态内容:

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

请帮忙

2 个答案:

答案 0 :(得分:4)

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

来自http://api.jquery.com/on/;

&#34;版本添加:1.7&#34;

答案 1 :(得分:0)

  $(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();
            }
        }
        });
    });    
相关问题