【问题标题】:jQuery's $() function always returns 'undefined' with AJAXjQuery 的 $() 函数总是使用 AJAX 返回“未定义”
【发布时间】:2009-02-26 01:45:33
【问题描述】:

我注意到弹出显示在文本框中的文本更新之前显示,我猜 js 在页面渲染之前被调用...这将解释“未定义”弹出...我如何确保 js 被调用页面渲染后?

重写以使其尽可能简单:

<body>
<form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:TextBox ID="txtRcaNotes" runat="server" TextMode="MultiLine" Width="800px"></asp:TextBox><br />
            <asp:Button ID="btnDoneWithRcs" runat="server" OnClick="btnDoneWithRcs_Click" Text="Action Completed / Update Notes" />
        </ContentTemplate>
    </asp:UpdatePanel>
</form>

<script type="text/javascript">

        var prm = Sys.WebForms.PageRequestManager.getInstance();
            prm.add_endRequest(
            function(){doStuff();}
            );

        function doStuff()
        {
            $(document).ready(function() {
                                $('txtRcaNotes').hide(); 
                                alert($('txtRcaNotes').attr('id'));
                                });
        }

</script>
</body>

代码背后:

protected void btnDoneWithRcs_Click(object sender, EventArgs e)
{
    txtRcaNotes.Text += "asdfadf";
}

文本框不会被隐藏,ALERT() 返回“未定义”

【问题讨论】:

    标签: asp.net jquery ajax pagerequestmanager


    【解决方案1】:

    您只是缺少 id 选择器语法。试试:

    $('#<%= txtRcaNotes.ClientID %>').hide(); 
    alert($('#<%= txtRcaNotes.ClientID %>').attr('id'));
    

    注意每个选择器前面添加的"#"

    【讨论】:

      【解决方案2】:

      您可以尝试的一件事是使用 Firebug 或其他一些 DOM 检查器,并在 AJAX 调用之前和之后检查 ASP.NET 生成的实际元素 ID,看看它们是否相同。

      【讨论】:

      • 这是我检查的第一件事,所有 ID 都匹配
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-21
      • 2012-01-08
      • 2018-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-16
      相关资源
      最近更新 更多