簡體   English   中英

嘗試創建一個輸入搜索,以使用合取和析取JQuery mobile過濾文本

[英]Trying to create an input search that filters text with conjunction and disjunction JQuery mobile

因此,我使用jquery mobile過濾了一個包含幾個段落的div,並且輸入搜索確實不允許我正在尋找的搜索類型。 使用以下代碼段,輸入搜索將針對輸入字段中輸入的內容精確過濾文本:

 <form class="ui-filterable">
        <input id="myFilter" data-type="search"></input>
    </form>
    <div data-filter="true" data-input="#myFilter">
    <p id="one">This is paragraph one or 1; it's not number 2</p>
    <p id="two">Next is two too 2</p>
    <p id="three">Three 3 is after 2</p>
    <p id="four">Finally is 4 four but not two</p>
    </div>

當前,“ paragraph 1”的輸入甚至都不會過濾為p#one,因為字符串“ paragraph 1”沒有確切地出現在文本中。 如何在搜索功能中添加合取和析取,以便能夠搜索僅包含““ paragraph” AND“ 2”“或”“ 3” OR“ 2”“的段落? 我認為我需要將regex與&&和||一起使用 為達到這個。

看看這個http://jsfiddle.net/jreh3zhc/7/

var $paras = $('p');

$('#myFilter').on('keyup', function () {
    var $this = $(this),
        vals = $this.val().toLowerCase().trim().split(' '),
        $found = $paras.filter(function (i, p) {
            var words = $(p).text().toLowerCase().split(' ');
            return vals.every(function (val) {
                return ~words.indexOf(val);
            });
        });
    $found.css('backgroundColor', 'red');
    $paras.not($found).css('backgroundColor', '');
});

對於OR而不是AND您應該使用Array.prototype.some()而不是每個

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM