jquery赢得't更新动态生成的html
jquery won't update dynamically generated html
我有一个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
相关文章:
- 如何使Javascript动态html表及其上的事件
- 使用AngularJS Directive WHITOUT$scope创建一个动态html元素
- Rails:如何在浏览器中显示动态html内容,而不会对其进行转义,也不会引起XSS攻击
- 如何使用jQueryAjax使用动态html输入texbox提交表单
- 使用Kendo的动态HTML元素绑定
- 使用jquery插入动态HTML后,单击不起作用
- jQuery.tmpl没有呈现动态html
- 使用jquery从JSON数据生成动态html
- jQuery自动完成显示不需要的动态html标记
- 使用JavaScript制作动态HTML页面以重定向到URL
- 在angular中,使用ng repeat生成动态html内容
- Dynatable与动态HTML标头断开
- 如何使用jQuery读取动态html表行
- 将点击函数添加到动态 HTML 链接
- 使用 Javascript 的动态 HTML:用不同的内容填充相同的 HTML 对象
- 如何在创建动态 HTML 页面时使用日期选择器
- 动态HTML元素-如何将表单提交到php
- JavaScript确认何时在表单中选择动态HTML字段
- 动态html表,但表头位于底部
- 如何在另一个java脚本函数中访问java脚本中动态html表中单元格的动态赋值