gpt4 book ai didi

asp.net-mvc - 更改表中给定行的背景颜色

转载 作者:行者123 更新时间:2023-12-01 00:20:13 24 4
gpt4 key购买 nike

我需要根据 @Html.CheckBox 中使用的模型( bool )属性的值更改表中给定行的背景颜色。该模型将使用 PostExampleCompleted 操作方法中的新复选框值进行更新。

<table>
<thead>
<tr>
<th>Item name</th>
<th>Comments</th>
<th>User</th>
<th>Complete</th>
</tr>
</thead>
<tbody>
<tr id="FooRow">
<td>Foo</td>
<td>@Model.FooComments</td>
<td>@Model.FooUserName</td>
<td>
@using (Ajax.BeginForm("PostFooCompleted", "Home", new AjaxOptions { OnBegin = "ShowProcessingMsg", OnComplete = "HideProcessingMsg" }))
{
@Html.CheckBox("FooItemComplete", Model.FooComplete, new { onClick = "$(this).parent('form:first').submit();" })
}
</td>
</tr>
<tr id="WidgetRow">
<td>Widget</td>
<td>@Model.WidgetComments</td>
<td>@Model.WidgetUserName</td>
@using (Ajax.BeginForm("PostWidgetCompleted", "Home", new AjaxOptions { OnBegin = "ShowProcessingMsg", OnComplete = "HideProcessingMsg" }))
{
@Html.CheckBox("WidgetItemComplete", Model.WidgetComplete, new { onClick = "$(this).parent('form:first').submit();" })
}
</td>
</tr>
</tbody>
</table>

实现这一目标的最佳方法是什么?代码示例将不胜感激:)。

谢谢。

最佳答案

鉴于我已经理解您想要正确执行的操作,这样的事情应该可以解决问题。

首先,这是一个 css 类,如果选中了复选框,我会用它来为行着色。

.redBackground
{
background-color: Red;
}

接下来,这里有一些 JQuery 代码,用于为复选框所在的行(如果选中)着色。我还为每个复选框添加了一个“更改”处理程序,因此如果其中任何一个复选框被更新,行颜色也会相应更新(我刚刚在选中复选框的行中使用了红色,而在未选中复选框)。

<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">

$(function () {

$('input[type=checkbox]').each(function () {

var checkbox = $(this);

// if the checkbox is already checked, colour its row
CheckStatus(checkbox);

//Every time a check-box status changes we
//want to re-evaluate the row colour
checkbox.change(function () {
CheckStatus(checkbox);
});

});

//Method which checks if the check-box is checked. If it's checked
//the row is given the 'redBackground' class, otherwise it is taken away
function CheckStatus(checkbox) {
if (checkbox.attr('checked') == 'checked') {
checkbox.parent().parent().addClass('redBackground');
}
else {
checkbox.parent().parent().removeClass('redBackground');
}
}

});
</script>

希望这是有道理的......:)

关于asp.net-mvc - 更改表中给定行的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7187808/

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