简体   繁体   中英

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. 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()

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());
                        }
                    });
      });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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