[英]jQuery - get next element inside checkbox each
I have a table with following columns: checkbox, text, text, ... For every selected checkbox I need to get the 2nd text value and test if contains some value. 我有一个带有以下列的表格:复选框,文本,文本,...对于每个选中的复选框,我需要获取第二个文本值并测试是否包含某个值。
My code so far 到目前为止我的代码
$('input:checkbox').each(function () {
var current = $(this);
if (current.is(':checked')) {
var txt = current.next('.inf-name').text();
if (txt.contains(inf) { ... }
}
});
razor code: 剃刀代码:
<table class="table table-bordered table-modified">
<thead>
<tr>
<th></th>
<th>State</th>
<th>Lookup Type</th>
<th>Plates</th>
<th>Notices</th>
</tr>
</thead>
<tbody>
@Html.HiddenFor(p => p.OrgUnitID, new { @Value = orgUnitId })
@for(var ind = 0; ind < records.Count(); ++ind)
{
var r = records[ind];
var i = ind;
@Html.HiddenFor(p => p.Comp[i].State, new { @Value = @r.State })
<tr>
<td>@Html.CheckBoxFor(p => p.Comp[i].Checked)</td>
<td>@r.State</td>
@if (dict.ContainsKey(r.State))
{ <td class="inf-name">@dict[r.State].ToString()</td> }
else
{ <td class="inf-name"></td> }
<td>@Html.ActionLink(@r.Plates.ToString(CultureInfo.InvariantCulture), "Plates", new { controller = "Lookup", state = @r.State})</td>
<td>@Html.ActionLink(@r.Notices.ToString(CultureInfo.InvariantCulture), "Plates", new { controller = "Lookup" }, new { state = @r.State })</td>
</tr>
}
</tbody>
</table>
where inf-name is the class on 2nd element. 其中inf-name是第二个元素上的类。 I can't get it done. 我做不完 Anyone know a solution? 有人知道解决方案吗?
using next()
will get the direct sibling , you need to get parent for check box which is a <td>
find the next next sibling using nextAll().eq(1)
, get the text inside the that sibling using .text()
使用next()
将获得直接的兄弟姐妹,你需要得到家长的复选框,这是一个<td>
找到使用下一个同级nextAll().eq(1)
获得使用该兄弟中的文本.text()
Edit : 编辑:
if you want your target using classes ( provided classes will never change later ) , then just change your code to be : 如果您希望目标使用类(提供的类以后不会更改),则只需将代码更改为:
$('input:checkbox').each(function () {
var current = $(this);
if (current.is(':checked')) {
var txt = current.next('.inf-name').eq(1).text();
if (txt.contains(inf) { ... }
}
});
var currentRows = $('.table tbody tr');
$.each(currentRows, function () {
$(this).find(':checkbox').each(function () {
if ($(this).is(':checked')) {
console.log($(currentRows).eq(1).val());
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.