简体   繁体   English

通过ul li筛选和搜索div文本,请单击

[英]Filter and search div text by ul li click

When you click the li , the div 's below should be filtered based on the selected li data values. 单击li ,应根据所选的li数据值过滤以下div This is working but for some reason, when you first load the page you have to click an li twice for the filter to work. 这是可行的,但是由于某种原因,当您首次加载页面时,必须单击一次li才能使过滤器起作用。

Codepin Example Codepin示例

<div class="dropdown">
    <a href="javascript:;" class="btn dropbtn selectBedrooms">select bedrooms <span class="icon downarrow"></span></a>

    <ul id='filterFloorplans' onClick='filterFloorplans()' class="btn dropbtn">
        <li data-value="1" class="dropdown-option">1 Bedroom</li>
        <li data-value="2" class="dropdown-option">2 Bedrooms</li>
        <li data-value="all" class="dropdown-option">all</li>
    </ul>
</div>

<div class="floorplanscontainer">
    <div class="unit">1 bedroom</div>
    <div class="unit">1 bedroom</div>
    <div class="unit">2 bedrooms</div>
</div>


<script>

    function filterFloorplans()
    {
        $('.dropdown-option').click(function () {
            var theValue = $(this).attr('data-value');

            var rex = new RegExp(theValue);

            if(rex =="/all/"){
                clearFilter()
            }else{
                $('.unit').hide();

                $('.unit').filter(function() {
                    return rex.test($(this).text());
                }).show();
            }
             $('.dropdown ul.on').removeClass("on");
        });
    }

    function clearFilter()
    {
        $('.filterFloorplans').val('');
        $('.unit').show();
    }

    $('.selectBedrooms').click(function () {
        $('.dropdown ul').toggleClass("on");
    });

</script>

for some reason, when you first load the page you have to click an li twice for the filter to work. 由于某种原因,当您第一次加载页面时,必须单击两次li才能使过滤器起作用。

That because the first click will attach the click event and second will fire it. 那是因为第一次点击将附加click事件,第二次将触发它。

You have to attach the event outside of the function and you don't need the function filterFloorplans() at all, remove the onClick : 您必须将事件附加到函数外部,并且根本不需要函数filterFloorplans() ,请删除onClick

 onClick='filterFloorplans()'

And define the click event out of the function, check the working example below. 并在函数之外定义click事件,请查看下面的工作示例。

Hope this helps. 希望这可以帮助。

 $(function(){ //ready function $('.dropdown-option').click(function () { var theValue = $(this).attr('data-value'); var rex = new RegExp(theValue); if(rex =="/all/"){clearFilter()}else{ $('.unit').hide(); $('.unit').filter(function() { return rex.test($(this).text()); }).show(); } $('.dropdown ul.on').removeClass("on"); }); function clearFilter() { $('.filterFloorplans').val(''); $('.unit').show(); } $('.selectBedrooms').click(function () { $('.dropdown ul').toggleClass("on"); }); }); 
 .dropdown ul li { width:100%; border:1px solid red; cursor: pointer; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="dropdown"> <a href="javascript:;" class="btn dropbtn selectBedrooms">select bedrooms <span class="icon downarrow"></span></a> <ul id='filterFloorplans' class="btn dropbtn"> <li data-value="1" class="dropdown-option">1 Bedroom</li> <li data-value="2" class="dropdown-option">2 Bedrooms</li> <li data-value="all" class="dropdown-option">all</li> </ul> </div> <div class="floorplanscontainer"> <div class="unit">1 bedroom</div> <div class="unit">1 bedroom</div> <div class="unit">2 bedrooms</div> </div> 

Instead of 代替

function filterFloorplans()
    {  
        // var rex = new RegExp($('#filterFloorplans').val());
        $('.dropdown-option').click(function () {
            var theValue = $(this).attr('data-value');

            var rex = new RegExp(theValue);

            if(rex =="/all/"){clearFilter()}else{
                    $('.unit').hide();
                    $('.unit').filter(function() {
                    return rex.test($(this).text());
                    }).show();
                }
             $('.dropdown ul.on').removeClass("on");
        });
    }

Write

$(function()
    {  
        // var rex = new RegExp($('#filterFloorplans').val());
        $('.dropdown-option').click(function () {
            var theValue = $(this).attr('data-value');

            var rex = new RegExp(theValue);

            if(rex =="/all/"){clearFilter()}else{
                    $('.unit').hide();
                    $('.unit').filter(function() {
                    return rex.test($(this).text());
                    }).show();
                }
             $('.dropdown ul.on').removeClass("on");
        });
    });

and remove the onClick in the ul element 并删除ul元素中的onClick

You don't need the filterFloorPlans function. 您不需要filterFloorPlans函数。 Your JS would be like this: 您的JS会像这样:

$(function() {
   $('.dropdown-option').click(function () {
      var theValue = $(this).attr('data-value');

      var rex = new RegExp(theValue);

      if(rex =="/all/"){clearFilter()}else{
        $('.unit').hide();
        $('.unit').filter(function() {
          return rex.test($(this).text());
        }).show();
      }
      $('.dropdown ul.on').removeClass("on");
    });

    function clearFilter()
    {
      $('.filterFloorplans').val('');
      $('.unit').show();
    }

    $('.selectBedrooms').click(function () {
      $('.dropdown ul').toggleClass("on");
    });
});

And your HTML: 而您的HTML:

<div class="dropdown">
  <a href="javascript:;" class="btn dropbtn selectBedrooms">select bedrooms <span class="icon downarrow"></span></a>
  <ul id='filterFloorplans' class="btn dropbtn">
    <li data-value="1" class="dropdown-option">1 Bedroom</li>
    <li data-value="2" class="dropdown-option">2 Bedrooms</li>
    <li data-value="all" class="dropdown-option">all</li>
  </ul>
</div>

<div class="floorplanscontainer">
  <div class="unit">1 bedroom</div>
  <div class="unit">1 bedroom</div>
  <div class="unit">2 bedrooms</div>
</div>

You can see here: http://codepen.io/anon/pen/XjzKkO 您可以在这里看到: http : //codepen.io/anon/pen/XjzKkO

JSBIN : JSBIN JSBIN: JSBIN

 <div class="dropdown">
          <a href="javascript:;" class="btn dropbtn selectBedrooms">select bedrooms <span class="icon downarrow"></span></a>
          <ul id='filterFloorplans' onClick='filterFloorplans()' class="btn dropbtn">
            <li data-value="1" class="dropdown-option">1 Bedroom</li>
            <li data-value="2" class="dropdown-option">2 Bedrooms</li>
            <li data-value="all" class="dropdown-option">all</li>
          </ul>

        </div>

JAVASCRIPT JAVASCRIPT

$('ul li').on('click',function(e){
  e.preventDefault();
  var theValue = $(this).attr('data-value');
  var rex = new RegExp(theValue);

                if(rex =="/all/"){
                   $('.filterFloorplans').val('');
                $('.unit').show();
                                 }else{
                        $('.unit').hide();
                        $('.unit').filter(function() {
                        return rex.test($(this).text());
                        }).show();
                    }
                 $('.dropdown ul.on').removeClass("on");
})
$('.selectBedrooms').click(function () {
                $('.dropdown ul').toggleClass("on");
            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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