简体   繁体   English

在 Javascript 或 jquery 中键入功能时进行搜索?

[英]Search as you type functionality in Javascript or jquery?

I'm trying to Implement search as you type feature and I'm not sure how to get substring after I type '@' in textarea.我正在尝试在您键入功能时实现搜索,但我不确定在 textarea 中键入“@”后如何获取子字符串。

Here is the fiddle what have I done https://jsfiddle.net/chille1987/zp3hx8ag/13/这是我做了什么的小提琴https://jsfiddle.net/chille1987/zp3hx8ag/13/

$('#text').on('input', function(e) {

    if(e.originalEvent.data == '@') {
      $('.list').show();
      let cursorPosition = $(this).prop('selectionStart')

    $(this).on('input', function(e) {

      if(e.originalEvent.data == ' ') {
        $('.list').hide();
      } else {
        let value = $(this).val();
        let textForSearch = value.substr(cursorPosition, value.indexOf(' '))
        console.log(textForSearch)

        $(".list li").each(function() {
          if ($(this).html().toLowerCase().indexOf(textForSearch) != -1) {
            $(this).show();
          }
          else {
            $(this).hide();  
          }
        });
      }

    });
  }

});
<textarea id="text"></textarea>

<ul class="list">
  <li>One</li>
  <li>Two</li>
  <li>Three</li>
  <li>Four</li>
  <li>Five</li>
</ul>
.list {
  display: none;
}

Trying to figure out how to get text in this line: value.substr(cursorPosition, value.indexOf(' ')) - trying to get substring from cursorPosition until first blank space after cursor position.试图弄清楚如何在这一行中获取文本: value.substr(cursorPosition, value.indexOf(' ')) - 尝试从 cursorPosition 获取子字符串,直到光标位置后的第一个空格。 I can type @ anywhere in the text not just at the end.我可以在文本中的任何地方输入@,而不仅仅是在末尾。

I made some changes on your own code and now its working.我对您自己的代码进行了一些更改,现在可以正常工作了。 this is how it works.这就是它的工作原理。 We trace user input char by char, if it was '@' we set cursorPosition to index of cursor.我们逐个字符地跟踪用户输入字符,如果它是'@',我们将 cursorPosition 设置为光标的索引。 else if was 'space', then we check : if text we have so far, is only spaces, just pass.否则,如果是“空格”,那么我们检查:如果到目前为止我们拥有的文本只是空格,则通过。 else textForSearch is substr of cursorPosition to current input index without leading and ending spaces (trim()). else textForSearch 是 cursorPosition 的 substr 到当前输入索引,没有前导和结尾空格 (trim())。 then we compare with li's text.然后我们与李的文字进行比较。

Js: JS:

let cursorPosition = -1;
$('#text').on('input', function(e) {
    if(e.originalEvent.data == '@') {
        $('.list li').show();
        cursorPosition = $(this).prop('selectionStart');
    }
    else if(e.originalEvent.data == ' ') {
        if (cursorPosition != -1 ){
            let value = $(this).val();
            let textForSearch = value.substr(cursorPosition,  
            $(this).prop('selectionStart')).trim().toLowerCase();
            if(! textForSearch){
                $('.list li').hide();
                return;
            }
            $(".list li").each(function(i, li){
                if($(li).html().toLowerCase().indexOf(textForSearch)) != -1) 
                {
                    $('.list').show();
                    $(li).show();
                }else {
                    $(li).hide();  
                }
            });
        }

        else{ 
              $('.list li').hide();
        }
     } 
  });

CSS: CSS:

.list li{
    display:none ;
}

I have modified the JS code我已经修改了 JS 代码

Try this javascript code instead of what you are using.试试这个 javascript 代码,而不是你正在使用的代码。 working fiddle https://jsfiddle.net/raghulramkish/9mq0pbhk/工作小提琴https://jsfiddle.net/raghulramkish/9mq0pbhk/

    var textForSearch="";
    var textareaValue="";
      $('ul.list li').click(function(e) 
        { 
        var val=$(this).text();
         textarea = document.getElementById('text').value;
          textarea = textarea + val;


     document.getElementById('text').value=
       textarea.replace("@"+textForSearch,"");
        });

On click on the list the above section will add the option to the text area (eg:instead of "@On" it will replace to One)单击列表时,上面的部分会将选项添加到文本区域(例如:而不是“@On”,它将替换为 One)

    $('#text').on('input', function(e) {

        if(e.originalEvent.data == '@') {
        $('.list').show();
        let cursorPosition = $(this).prop('selectionStart')

        $(this).on('input', function(e) {

          if(e.originalEvent.data == ' ') {
            $('.list').hide();
          } 
          else {
            let value = $(this).val();
             textForSearch = value.substr(cursorPosition, value.indexOf('@'))


            $(".list li").each(function() {
              if ($(this).html().toLowerCase().indexOf(textForSearch) != -1) {

                $(this).show();
              }
              else {
                $(this).hide();  
              }
                });
          }




        });
      }

    });

Instead of using " textForSearch = value.substr(cursorPosition, value.indexOf(' '))" use "textForSearch = value.substr(cursorPosition, value.indexOf('@'))".不要使用“ textForSearch = value.substr(cursorPosition, value.indexOf(' '))”,而是使用“textForSearch = value.substr(cursorPosition, value.indexOf('@'))”。

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

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