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