gpt4 book ai didi

javascript - 更改 jquery ajax 表中一行的字体颜色

转载 作者:行者123 更新时间:2023-11-28 02:27:24 29 4
gpt4 key购买 nike

我想根据函数内部应用的条件更改一行中值的字体颜色。如果 TotalStudent 大于房间容量,则将学生信息添加到表格中,字体颜色为红色。以下是我尝试过的。我使用了 ajax 方法,在成功函数中我正在生成表来插入值。状况良好。

Create.cshtml

$.ajax({
type: "POST",
url: '@Url.Action("AddStudent", "Student")',
data: model,
dataType: "json",
success: function(data) {
$('#dialog').dialog('close');
alert(data.countstudent);
alert(data.roomcapacity);
//var student = data.countstudent;
if (data.countstudent > data.roomcapacity) {
var tblEndrolled = $("#tblEnrolled");
$.each(data.record, function(index, item) {
$('.status').css("color", "red");
var tr = $("<tr></tr>");
tr.html(("<td>" + '<input type="submit" id="' + item.Id + '" value="Remove" class="" />' + "</td>") +
" " + ("<td class = 'status'>" + item.FirstName + "</td>") +
" " + ("<td>" + item.LastName + "</td>") +
" " + ("<td>" + item.EmailAddress + "</td>") +
" " + ("<td>" + item.Phone + "</td>"));
tblEndrolled.append(tr);
});
}

<div>
<strong style = "font-size:20px" > Enrolled Students: < /strong>
<table class = "table table-bordered table-responsive table-hover" id="tblEnrolled">
<tr>
<th>Action</th>
@*<th>User Id</th>
*@<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Phone</th>
</tr>
</table>
</div>

最佳答案

它需要两个修改:

  1. 第一,在创建 tr 时添加类状态。
  2. 第二次在每个循环结束时应用 css 样式,如下所示:

.

//var student = data.countstudent;
if (data.countstudent > data.roomcapacity) {
var tblEndrolled = $("#tblEnrolled");
$.each(data.record, function(index, item) {
var tr = $("<tr class = 'status'></tr>");
tr.html(("<td>" + '<input type="submit" id="' + item.Id + '" value="Remove" class="" />' + "</td>") +
" " + ("<td >" + item.FirstName + "</td>") +
" " + ("<td>" + item.LastName + "</td>") +
" " + ("<td>" + item.EmailAddress + "</td>") +
" " + ("<td>" + item.Phone + "</td>"));
tblEndrolled.append(tr);
});
$('.status').css("color", "red");
}

希望它能满足您的要求。

关于javascript - 更改 jquery ajax 表中一行的字体颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47879973/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com