Javascript jquery赢得';t更新动态生成的html
我有一个ajax函数,可以加载我的收件箱消息,每条消息都有一个用户类型和读取字段 我正在对消息进行循环,并为它们生成htmlJavascript jquery赢得';t更新动态生成的html,javascript,jquery,ajax,json,Javascript,Jquery,Ajax,Json,我有一个ajax函数,可以加载我的收件箱消息,每条消息都有一个用户类型和读取字段 我正在对消息进行循环,并为它们生成html function initializeMailbox() { // get all mailbox data user.GetInboxMessages(function (response) { if (response) { inboxMessages['inbox'] = response;
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++;
}
}
函数初始化EmailBox(){
//获取所有邮箱数据
user.GetInboxMessages(函数(响应){
如果(答复){
inboxMessages['inbox']=响应;
$(“#收件箱表”).fadeIn();
loadInboxTable();
inboxDataTable=$(“#inboxTable”).dataTable();
$(“#收件箱计数”).html(收件箱信息计数);
显示消息(第一个消息id);
}
});
}
函数loadInboxTable(){
对于(var i=0;i”+
"" +
“
”+
“”+用户类型+“”+
“”+
“”+inboxMessages['inbox'][i]。名字+“”+inboxMessages['inbox'][i]。名字+“
”+
“+inboxMessages['inbox'][i]。主题+”
”+
“+inboxMessages['inbox'][i].datesent.toString().split(”[0]+“+
"" +
"" +
""
);
//检查消息是否已被读取
如果(inboxMessages['inbox'][i]。读取==0){
//不读
$(“#消息主题”).addClass('read-message');
}否则{
//阅读
$(“#消息主题”).removeClass('read-message');
}
收件箱_msg_count++;
}
}
现在,如果我提醒出user_type的值并读取,我将根据它迭代的消息获得正确的值。但是当它输出时,它只使用第一条消息的值
我需要能够基于这些值使用jquery动态地设置消息的样式。有人能告诉我为什么这不起作用吗…好吧,首先,您使用的是ID选择器:
$(“#消息主题”).addClass('read-message')代码>
当您实际拥有一个类时:
嗯,首先,您使用的是一个ID选择器:
$(“#消息主题”).addClass('read-message')代码>
当您实际拥有一个类时:
在您的代码中,我认为您打算执行以下操作
如果(收件箱消息['inbox'][i]。用户类型==1)
注意等号。您当前拥有的将始终是真实的,用户类型
将始终分配给您代码中的捐赠者
,我认为您打算执行以下操作
如果(收件箱消息['inbox'][i]。用户类型==1)
注意等号。您当前拥有的将始终为真,user\u type
将始终分配给DONOR
我不确定您的DOM是否有效,因为您正在添加具有相同ID(“user\u type”和“read”)的多个元素。ID必须是唯一的。我不确定您的DOM是否有效,因为您正在添加具有相同ID的多个元素(“用户类型”和“读取”)。ID必须是唯一的。如果它们是整数,为了在javascript中的安全,您应该使用===,但您的权利,这只是一个赋值语句!如果它们是整数,为了javascript中的安全,您应该使用==,但您的权利是,这只是一个赋值语句!哇…这就是为什么我不应该在早上5点写代码,不先睡觉。谢谢。哇……这就是为什么我不应该在早上5点写代码,不先睡觉。非常感谢。
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 ];
...
<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='...