jquery赢得't更新动态生成的html

jquery won't update dynamically generated html

本文关键字:动态 html 更新 赢得 jquery      更新时间:2023-09-26

我有一个ajax函数,用于加载收件箱消息,每条消息都有一个user_type和read字段。

我正在对消息进行循环,并为它们生成html。

function initializeMailbox() {
    // get all mailbox data
    user.GetInboxMessages(function (response) {
        if (response) {
            inboxMessages['inbox'] = response;
            $("#inbox-table").fadeIn();
            loadInboxTable();
            inboxDataTable = $("#inboxTable").dataTable();
            $("#inbox-count").html(inbox_msg_count);
            displayMessage(first_msg_id);
        }
    });
}
function loadInboxTable() {
    for (var i = 0; i < inboxMessages['inbox'].length - 1; i++) {
        first_msg_id = inboxMessages['inbox'][0].message_id;
        var user_type = "";
        if (inboxMessages['inbox'][i].user_type = 1)
            user_type = "DONOR";
        else if (inboxMessages['inbox'][i].user_type = 0)
            user_type = "CANDIDATE";
        else if (inboxMessages['inbox'][i].user_type = 2)
            user_type = "GROUP";
        $("#inbox-table-body").append(
            "<tr class='data-row' style='height: 75px;'> " +
                "<td>" +
                "<input type='hidden' id='user_type' value='" + inboxMessages['inbox'][i].user_type + "'/>" +
                "<input type='hidden' id='read' value='" + inboxMessages['inbox'][i].read + "'/>" +
                "<input type='checkbox' id='" + inboxMessages['inbox'][i].message_id + "'></input></td>" +
                "<td>" +
                "<p class='left'>" +
                "<img class='td-avatar' style='margin-top: 0px !important;' src='/uploads/profile-pictures/" + inboxMessages['inbox'][i].image + "' alt='avatar'/>" +
                "<br/>" +
                "<span class='user-type'>" + user_type + "</span>" +
                "</p></td><td>" +
                "<h2 onclick='displayMessage(" + inboxMessages['inbox'][i].message_id + ");'>" + inboxMessages['inbox'][i].firstname + " " + inboxMessages['inbox'][i].lastname + "</h2><br/>" +
                "<h3 class='message-subject' onclick='displayMessage(" + inboxMessages['inbox'][i].message_id + ");'>" + inboxMessages['inbox'][i].subject + "</h3><br/><br/>" +
                "<h3 style='font-size: 0.7em; margin-top: -25px; float:left;'><span>" + inboxMessages['inbox'][i].datesent.toString().split(" ")[0] + "</span></h3>" +
                "</td>" +
                "<td><button class='delete-item' onclick='deleteMessage(" + inboxMessages['inbox'][i].message_id + ");' src='/images/delete-item.gif' alt='Delete Message' title='Delete Message' style='cursor: pointer; float:left; margin-left: 5px; margin-top:-3px;'></button></td>" +
                "</tr>"
        );
        // check if the message has been read
        if (inboxMessages['inbox'][i].read == 0) {
            // not read
            $("#message-subject").addClass('read-message');
        } else {
            // read
            $("#message-subject").removeClass('read-message');
        }
        inbox_msg_count++;
    }
}

现在,如果我提醒user_type的值并读取,我就会根据它迭代的消息得到正确的值。但当它输出时,它只使用第一条消息的值。

我需要能够基于这些值,使用jquery动态地设置消息样式。有人能告诉我为什么这不起作用吗。。。

好吧,有一件事,您正在使用一个ID选择器:

$("#message-subject").addClass('read-message');

当你真的有一个类:

<h3 class='message-subject'...

用途:

$(".message-subject").addClass('read-message');

其次,您正在进行赋值(=),而不是对user_type进行比较(==)。

我可以建议一个不同的方法而不是一个大的if..then..else吗?

使用数组对用户类型进行索引:

var user_type_labels = [ 'CANDIDATE', 'DONOR', 'GROUP' ];
function loadInboxTable() {
    for (var i = 0; i < inboxMessages['inbox'].length - 1; i++) {
        first_msg_id = inboxMessages['inbox'][0].message_id;
        // One line instead of an if/then/else
        var user_type = user_type_labels[ inboxMessages['inbox'][i].user_type ];
        ...

第三个,您将向DOM中添加多个具有相同ID的项。这是不合法的,后果不明。

<input type='hidden' id='user_type' value='...
<input type='hidden' id='read' value='...

为此,您需要使用类。

<input type='hidden' class='user_type' value='...
<input type='hidden' class='read' value='...

在您的代码中,我认为您打算执行以下

if(inboxMessages['inbox'][i].user_type===1)

注意等号。您当前拥有的内容将始终为真,user_type将始终分配给DONOR