Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/459.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用indexOf匹配的JS代码是如何工作的?_Javascript_Jquery_Html_Datatables - Fatal编程技术网

Javascript 使用indexOf匹配的JS代码是如何工作的?

Javascript 使用indexOf匹配的JS代码是如何工作的?,javascript,jquery,html,datatables,Javascript,Jquery,Html,Datatables,我已经设法将一些JS复制到我的文档中,并且我已经让它工作了。但我不完全明白它是怎么做到的 它是一个搜索函数,用于匹配表中的数据并隐藏任何不匹配的行 但我不理解实际搜索和匹配的活动代码行。有人能解释一下吗 $(“#搜索栏”).keyup(函数(){ searchFunction($(this.val()); }); 函数搜索函数(值){ $('#results tr')。每个(函数(){ var found='false'; $(this).each(function(){ if($(this).

我已经设法将一些JS复制到我的文档中,并且我已经让它工作了。但我不完全明白它是怎么做到的

它是一个搜索函数,用于匹配表中的数据并隐藏任何不匹配的行

但我不理解实际搜索和匹配的活动代码行。有人能解释一下吗

$(“#搜索栏”).keyup(函数(){
searchFunction($(this.val());
});
函数搜索函数(值){
$('#results tr')。每个(函数(){
var found='false';
$(this).each(function(){
if($(this).text().toLowerCase().indexOf(value.toLowerCase())>=0){
found='true';
}
});
如果(找到=='true'){
$(this.show();
}否则{
$(this.hide();
}
})
};
这是我无法理解的一句话:

if($(this).text().toLowerCase().indexOf(value.toLowerCase())>=0){
found='true';
}

我理解它如何将变量更改为true,但我不理解它如何将表行中的数据与输入的值匹配。

它将发送给函数的值转换为小写,然后查看行中的数据。它也将其转换为小写,并使用indexof查看是否存在匹配,此处将介绍:

基本上,indexOf()方法返回字符串中指定值第一次出现的位置。如果要搜索的值未出现,则返回-1

考虑搜索“测试”

n的结果将是:16,因此,正如在脚本中一样,大于0。。。和“发现”

它的作用是

对于“结果”表中的每一行
如果所有这些值中有一个(我用小写字母表示)与我在“搜索栏”中用小写字母输入的值相等不止一次,那么我找到了它,因此搜索栏中的find=“true”

按键事件将被触发,搜索栏的值将传递给搜索功能

$("#searchBar").keyup(function() {
  searchFunction($(this).val());
});

function searchFunction(value) {
  //value will contain the value of search bar
  $("#results tr").each(function() {
    //assuming value is not there in tr
    var found = "false";
    //now searching for each tr for value
    $(this).each(function() {
      //converting to lower case and comparing each value with searchbar value
      if (
        $(this)
          .text()
          .toLowerCase()
          .indexOf(value.toLowerCase()) >= 0
      ) {
        found = "true";
      }
    });
    //actual showing/hiding row
    if (found === "true") {
      $(this).show();
    } else {
      $(this).hide();
    }
  });
}
如果需要更多关于索引的信息
@MattCouthon如果您还需要什么,请告诉我

指的是
。代码将行中的所有文本作为一个长字符串,将其小写,然后对其调用
indexOf()
,并传递值。这将返回字符串中值的位置,如果找不到,则返回
-1
。请重命名与手头问题相关的标题-目前,这对想知道是否可以帮助的读者来说不是很有用。您的答案让人困惑,尤其是关于
的部分。
$("#searchBar").keyup(function() {
  searchFunction($(this).val());
});

function searchFunction(value) {
  //value will contain the value of search bar
  $("#results tr").each(function() {
    //assuming value is not there in tr
    var found = "false";
    //now searching for each tr for value
    $(this).each(function() {
      //converting to lower case and comparing each value with searchbar value
      if (
        $(this)
          .text()
          .toLowerCase()
          .indexOf(value.toLowerCase()) >= 0
      ) {
        found = "true";
      }
    });
    //actual showing/hiding row
    if (found === "true") {
      $(this).show();
    } else {
      $(this).hide();
    }
  });
}