简体   繁体   中英

toggle tr if attr is ^=X and X.length + 1

Need help with javascript condition. I'd like upon click to toggle a table row (tr) and subsequent (tr) where conditions are met.

I have the code working thus far but am not sure how to add in this additional condition. This additional condition is if sortString is X characters long, and subsequent tr have a sortString which are X+1, X+2, X+3, etc characters long to only toggle sortString.length + 1. IE only toggle length 4 if length 3 was clicked, and not toggle anything greater than length 4 (in this example).

$('tr').click(function () {
    var sortString = $(this).closest('tr').attr('sort'); // works
    $('tr[sort=' + sortString + ']').toggle(); // works
    $('tr[sort^=' + sortString + ']').toggle(); // needs additional condition
}

My sortString has characters concatenated onto it. So it looks like 12, or 123, or 1234, or 12345, etc. So if I clicked on a tr with a sortString of 123, then only tr with 1234 are toggled, and not 12345 (etc.)

I am guessing something like this...

$('tr[sort^=' + sortString + ']').(sortString.length + 1).toggle();

You probably want to iterate your second selection so you can filter by an extra if statement.

$('tr[sort^=' + sortString + ']').each() {
    if ($(this).attr('sort').length == sortString.length + 1) {
        $(this).toggle();
    }
}

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