jquery - 使用 Jquery 在 foreach MVC Razor 表中定位特定 td
问题描述
我有一个为用户显示不同内容的表格。每行的最后一个 td 包含一个 Html.ActionLink 和一个隐藏的 ajax 加载器。当按下选择按钮时,我希望只显示该行加载器 gif。按下 select 时,我可以很容易地显示每一个,但我正在努力将 td 隔离到单个 td 并只显示那个 ONE 加载器。这是表格代码:
<table class="table" id="tblCifSelection">
<tr>
<th>
@Html.DisplayNameFor(model => model.ClientName)
</th>
<th>
@Html.DisplayNameFor(model => model.PolicyNumber)
</th>
<th>
@Html.DisplayNameFor(model => model.PolicyTypeDescription)
</th>
<th>
@Html.DisplayNameFor(model => model.EffDate)
</th>
<th>
@Html.DisplayNameFor(model => model.ExpDate)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(model => item.ClientName)
</td>
<td>
@Html.DisplayFor(modelItem => item.PolicyNumber)
</td>
<td>
@Html.DisplayFor(modelItem => item.PolicyTypeDescription)
</td>
<td>
@Html.DisplayFor(modelItem => item.EffDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.ExpDate)
</td>
<td>
@Html.ActionLink("Select", "CompleteCIF", new { UniqPolicy = item.UniqPolicy }, new { onclick = "loadFunction();"}) |
@* @Html.ActionLink("Delete", "Delete", new { id=item.UniqPolicy })*@
<img id="cifLoader" class="ml-2 loader" src="~/Images/ajax-loader (3).gif" />
</td>
</tr>
}
这是我正在使用的脚本。
<script>
$(document).ready(function () {
$('.loader').hide();
})
function loadFunction() {
$(this).closest('.loader').show();
/* These are tests I ran to see what would work and what wouldn't */
//$('tr').closest('td').attr(' .loader').show();
//$('.loader').show(); //this shows all loaders. Only one that works.
$('#tblCifSelection').closest('tr').children('td:nth-child(6) img#cifLoader').show();
$(this).closest('tr').children('td:nth-child(6)').show();
}
</script>
在此先感谢您的帮助。
解决方案
感谢以上两位的帮助。我能够制定出可行的解决方案。表数据是一样的,不过我改了一小块..
<td>
@Html.ActionLink("Select", "CompleteCIF", new { UniqPolicy = item.UniqPolicy }, new { @onclick = "loaderFunction(this);"}) |
<img id="cifLoader" class="ml-2 loader" src="~/Images/ajax-loader (3).gif" />
</td>
function loaderFunction(t) {
$(t).closest('tr').find('td .loader').show();
//$('.loader').show(); //this shows all loaders
}
这完美地工作。仅在按下所选按钮的 tr 上显示加载程序。
推荐阅读
- php - 如何使用 PHP 将文本区域数据从 HTML 文档发送到 MySQL 数据库?
- python - 在TensorFlow中使用lstm按时间序列进行预测,而我的结果可能如此糟糕和荒谬
- android - Flutter - SingleScrollView 不在堆栈/列内滚动
- ios - 如何从 Dev、Test、Staging 和 Prod 环境发送 APNS 推送通知?
- python-3.x - 如何使用此深度优先搜索代码来获得拓扑排序?
- angular - 如何将亚马逊会员服务集成到我的 Angular 应用程序中?
- reactjs - react.js 重定向内部函数
- hive - hive metadatastore 会自动发现新添加的数据吗?
- python - 我的 CSV 数据看起来不错,但是当我绘制图表时,数据似乎不正确
- python - 无法使用原子编辑器运行 linter-flake8