开发者

jquery not selector with .data

I can't seem to figure out what i'm doing wrong with the following code:

            $('#side_categories #sourcestoggle ul#side_categories_prog .showonly').click(function () {
        var cattest2 = $(this).data('dealcat'); //get the category from the clicked button
        $('#listings .deal:not(:has(.deal[data-dealcat*="' +cattest2+ '"]))').addClass('filtered');
        $('#sourcestoggle ul#side_categories_prog li .check_box input:not([data-dealcat*="' +cattest2+ '"]').removeAttr('checked');
        $('#listings .deal[data-dealcat*="' +cattest2+ '"]').removeClass('filtered');
        $('#dealcount .counter #showingval').html(parseInt($('#listings .deal:visible').size()));
        });

it works to show ONLY the selected category based on data-dealcat, however it also unchecks all checkboxes, I can't seem to figure out how to keep the current checkbox checked. Since I'm new to jquery (and coding in general), i'm pretty certain the code is fairly sl开发者_Go百科oppy but it still works for the most part, just the checkbox which has the "only" link on it doesn't stay checked.

Thank you all in advance!

edit: the above code now keeps the first checked box, but when u click the others it doesn't do anything.


Try this

$('#side_categories #sourcestoggle ul#side_categories_prog .showonly').click(function () {
        var cattest2 = $(this).data('dealcat'); //get the category from the clicked button
        var checked = $('#sourcestoggle ul#side_categories_prog li .check_box input.deal[data-dealcat*="' +cattest2+ '"]').is(':checked');

        $('#listings .deal:not(:has(.deal[data-dealcat*="' +cattest2+ '"]))').addClass('filtered');

        $('#sourcestoggle ul#side_categories_prog li .check_box input:not(.deal[data-dealcat*="' +cattest2+ '"])').removeAttr('checked');

        $('#sourcestoggle ul#side_categories_prog li .check_box input.deal[data-dealcat*="' +cattest2+ '"]').attr('checked', checked);

        $('#listings .deal[data-dealcat*="' +cattest2+ '"]').removeClass('filtered');
        $('#dealcount .counter #showingval').html(parseInt($('#listings .deal:visible').size()));
        });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜