首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ReferenceError:未定义getMessage

ReferenceError:未定义getMessage
EN

Stack Overflow用户
提问于 2012-12-13 06:17:50
回答 1查看 526关注 0票数 0

我正在构建一个类似于facebook的消息传递区域,我正在使用带有jquery的ajax和一个asmx web服务来为客户端提供html。我的li单击事件在第一次使用c#加载页面内容时起作用,但是当ajax运行并刷新web服务中的内容时,li事件不再起作用。

这是从web服务返回的html的一个示例。

代码语言:javascript
复制
<ol class="messagesrow" id="messages">
<li id="2345">
<div>Test Element</div>
</li>
</ol>

Web服务标记

代码语言:javascript
复制
[WebMethod]
public string GetMessagesByObject(string id, string objectid, string userid, string timezone)
{
    string output = string.Empty;

    try
    {
        StringBuilder str = new StringBuilder();

        DataSet results = results from store procedure

        str.Append("<ol class=\"messagesrow\" id=\"messages\">");
        for (int i = 0; i < results.Tables[0].Rows.Count; i++)
        {
            DataRow row = results.Tables[0].Rows[i];

            DateTime date = Convert.ToDateTime(row["CreateDate"].ToString()).AddHours(Convert.ToDouble(timezone));

            if (!TDG.Common.CheckStringForEmpty(row["ParentMessageID"].ToString()))
            {
                str.Append("<li id=\"" + row["ParentMessageID"].ToString() + "\">");
            }
            else
            {
                str.Append("<li id=\"" + row["MessageID"].ToString() + "\">");
            }

            str.Append("<div style=\"width:100%; cursor:pointer;\">");

            str.Append("<div style=\"float:left; width:25%;\">");
            if (!TDG.Common.CheckStringForEmpty(row["ImageID"].ToString()))
            {
                str.Append("<img src=\"/Resources/getThumbnailImage.ashx?w=48&h=48&id=" + row["ImageID"].ToString() + "\" alt=\"View Profile\" />");
            }
            else
            {
                str.Append("<img src=\"/images/noProfileImage.gif\" alt=\"View Profile\" />");
            }
            str.Append("</div>");

            str.Append("<div style=\"float:left; width:75%; padding-top:4px;\">");
            str.Append("<strong>" + row["WholeName"].ToString() + "</strong>");
            str.Append("<br />");
            if (row["BodyMessage"].ToString().Length < 35)
            {
                str.Append("<span class=\"smallText\">" + row["BodyMessage"].ToString() + "</span>");
            }
            else
            {
                str.Append("<span class=\"smallText\">" + row["BodyMessage"].ToString().Substring(0, 35) + "</span>");
            }
            str.Append("<br /><span class=\"smallGreyText\">" + String.Format("{0:g}", date) + "</span>");
            str.Append("</div>");

            str.Append("</div>");
            str.Append("</li>");
        }
        str.Append("</ol>");

        output = str.ToString();
    }
    catch (Exception ex)
    {
        throw ex;
    }

    return output;
}

Jquery标记

代码语言:javascript
复制
$(document).ready(function () {        

    $("ol#messages li").click(function () {
        var id = $(this).attr("id");

        getMessage(id);
    });
});

function getMessage(id) {

        var timezone = $('#<%= hdfTimezone.ClientID %>').val()
        var userid = $('#<%= hdfUserID.ClientID %>').val()

        $.ajax({
            type: "POST",
            async: false,
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            url: "/Resources/MessageWebService.asmx/GetMessage",
            data: "{'id':'" + id + "','timezone':'" + timezone + "','userid':'" + userid + "' }",
            success: function (data) { 
                $('#<%= hdfMessageID.ClientID %>').val(id);
                $('#<%= ltlMessages.ClientID %>').html(data.d);
            },
            error: function (data) {
                showError(data.responseText);
            }
        });

    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-13 06:36:06

因为您的列表项是动态的,所以您应该从ol委托事件。

代码语言:javascript
复制
$(document).ready(function () {        

    $("#messages").delegate("li","click",function () {
        getMessage(this.id);
    });

});

对于给定的代码,您得到的ReferenceError: getMessage not defined错误应该不会发生。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13849676

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档