繁体   English   中英

jQuery-获取复选框内的下一个元素

[英]jQuery - get next element inside checkbox each

我有一个带有以下列的表格:复选框,文本,文本,...对于每个选中的复选框,我需要获取第二个文本值并测试是否包含某个值。

到目前为止我的代码

$('input:checkbox').each(function () {
    var current = $(this);

    if (current.is(':checked')) {
        var txt = current.next('.inf-name').text();
        if (txt.contains(inf) { ... }
    }
});

剃刀代码:

<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>

其中inf-name是第二个元素上的类。 我做不完 有人知道解决方案吗?

使用next()将获得直接的兄弟姐妹,你需要得到家长的复选框,这是一个<td>找到使用下一个同级nextAll().eq(1)获得使用该兄弟中的文本.text()

编辑:

如果您希望目标使用类(提供的类以后不会更改),则只需将代码更改为:

$('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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM