duanlv2788 2018-03-03 06:35
浏览 49
已采纳

搜索javascript时的逻辑问题

I have a text box to search for keywords that is present in divs. I am expecting a (and) kind of working while searching & the end result is I get the parent class names.

For instance:

input : google,facebook

output : p1

Issue : When i have 2 same keywords occurring in the same div then the whole logic fails.

Here is the code :

<input type="text" id="k_search">
<div class="p1">
    <div class="keyword">google is a search engne</div>
    <div class="keyword">facebook is sm site</div>
    <div class="keyword">orkut is no more</div>
</div>
<div class="p2">
    <div class="keyword">google is a gaint</div>           
    <!-- everything works fine until i have 2 keywords appearing inside 
       same parent div like this           
    <div class="keyword">google has gmail</div>
        // -->
</div>

Here is the javascript that i came up with :

<script>
    jQuery(document).ready(function($) {
        $('#k_search').keyup(function() {    
            var sel = [];       
            var sel_text = $(this).val();
            var search_arry = sel_text.split(',');      
            var newArray = search_arry.filter(function(v){return v!==''});      
            for(var i = 0;i<newArray.length;i++){           
                $(".keyword").each(function() {             
                    var cmp = $(this).text().toLowerCase();
                    var s_written = newArray[i].toLowerCase()
                    if(cmp.indexOf(s_written) > -1)
                    {
                        var temp  = $(this).parent().prop('className');
                        sel.push(temp);                 
                    }
                });
            }
            counts = {};
            new_sel = [];
            if(sel.length>1){
                /* Counting occurence of class names*/
                jQuery.each(sel, function(key,value) {
                    if (!counts.hasOwnProperty(value)) {
                        counts[value] = 1;
                    } else {
                        counts[value]++;
                    }
                });

                jQuery.each(counts, function(key,value) {
                    if(value == newArray.length)
                    {
                        new_sel.push(key);
                    }
                });
                process(new_sel);
            } else {
                process(sel);
            }
        });
    });
</script>

Here is the fiddle : https://jsfiddle.net/3q3ajusL/7/

Can you help please?

  • 写回答

2条回答 默认 最新

  • dongpu42006096 2018-03-03 23:24
    关注

    Based on your requirements, this should find what you were looking for. It was getting hard to understand the code, so I did it slightly differently.

    jQuery(document).ready(function($) {
        $('#k_search').keyup(function() {
            var keywords = $(this).val().toLowerCase().split(',').filter(v => !!v);
    
            var $divs = $('div:has(>.keyword)').filter((index, div) => {
                var text = $(div).find('.keyword')
                    .map((index, keyword) => $(keyword).text().toLowerCase())
                    .toArray().join('
    ');
    
                for (const keyword of keywords) {
                    if (!~text.indexOf(keyword)) {
                        return false;
                    }
                }
    
                return true;
            });
    
            $("#result").text($divs.map((index, div) => $(div).prop('className')).toArray());
        });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 pyqt怎么把滑块和输入框相互绑定,求解决!
  • ¥20 wpf datagrid单元闪烁效果失灵
  • ¥15 券商软件上市公司信息获取问题
  • ¥100 ensp启动设备蓝屏,代码clock_watchdog_timeout
  • ¥15 Android studio AVD启动不了
  • ¥15 陆空双模式无人机怎么做
  • ¥15 想咨询点问题,与算法转换,负荷预测,数字孪生有关
  • ¥15 C#中的编译平台的区别影响
  • ¥15 软件供应链安全是跟可靠性有关还是跟安全性有关?
  • ¥15 电脑蓝屏logfilessrtsrttrail问题