Bootstrap 4 cards filtering with jQuery

房东的猫 提交于 2020-01-14 05:54:05

问题


Someone has had a dig at this before, but I don't seem to be able to make it work. I'm trying to filter cards on Bootstrap 4 and when I apply a search query, it ends up hiding all my cards, instead of just the ones it should.

Hopefully anyone can help.

  $('#search').keyup(function() {
    $('.card').show();
    var filter = $(this).val(); // get the value of the input, which we filter on
    $('.card-deck').find('.card .card-body h4:not(:contains(" + filter + "))').parent().parent().addClass('d-none');
  })

Here's the full code: https://jsfiddle.net/8120104x/#&togetherjs=jTm0prUR4S


回答1:


The problem is that $('.card').show(); won't work because once d-none is added it overrides the display:block added by $('.card').show();. Each keyup should remove the d-none class instead...

$('#search').keyup(function (){
    $('.card').removeClass('d-none');
    var filter = $(this).val(); // get the value of the input, which we filter on
    $('.card-deck').find('.card .card-body h4:not(:contains("'+filter+'"))').parent().parent().addClass('d-none');
})

Working demo on Codeply

Note: jQuery :contains is case-sensitive.



来源:https://stackoverflow.com/questions/50005223/bootstrap-4-cards-filtering-with-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!