Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.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 用JS和jQuery搜索HTML表_Javascript_Jquery_Html_Zurb Foundation - Fatal编程技术网

Javascript 用JS和jQuery搜索HTML表

Javascript 用JS和jQuery搜索HTML表,javascript,jquery,html,zurb-foundation,Javascript,Jquery,Html,Zurb Foundation,我做了一张桌子,想让它能被搜索到,所以我在谷歌上搜索了一下starckoverflow 但不知怎的,我发现的东西,应该有用的,对我不有用 下面是代码,包括HTML和JS 魔兽世界人物。 //当文档准备就绪时:在body onload之前触发该命令 $(文档).ready(函数(){ //写入关键字输入元素的keyup事件 $(“搜索”).keyup(函数(){ //当输入值不为空时 if($(this.val()!=“”) { //仅显示匹配的TR,隐藏其余TR $(“#table tbod


我做了一张桌子,想让它能被搜索到,所以我在谷歌上搜索了一下starckoverflow
但不知怎的,我发现的东西,应该有用的,对我不有用

下面是代码,包括HTML和JS


魔兽世界人物。
//当文档准备就绪时:在body onload之前触发该命令
$(文档).ready(函数(){
//写入关键字输入元素的keyup事件
$(“搜索”).keyup(函数(){
//当输入值不为空时
if($(this.val()!=“”)
{
//仅显示匹配的TR,隐藏其余TR
$(“#table tbody tr”).hide();
$(“#表td:包含ci(“+$(this.val()+”)))”).parent(“tr”).show();
}
其他的
{
//当没有输入或再次清理时,显示所有内容
$(“#table tbody tr”).show();
}
});
});
//不区分大小写筛选器的jQuery表达式
$.extend($.expr[“:”],
{
“包含ci”:函数(元素、i、匹配、数组)
{
return(elem.textContent | | | elem.innerText | |$(elem.text()| |“”).toLowerCase().indexOf((匹配[3]| |“”).toLowerCase())>=0;
}
});
魔兽世界人物。我和我的兄弟们,我们分享。
字符名
等级
王国
本杰明。
流氓。
奥杜姆。
卡乔托。
猎人
阿加马根。
Contemplario。
圣骑士。
奥杜姆。
艾尔特龙。
死亡骑士。
阿加马根斯。
基洛。
牧师
阿加马根。
基蒂亚拉莫克。
战士
阿加马根。
马格斯特罗。
法师。
阿加马根。
马塞勒斯。
法师。
奥杜姆。
米斯特拉。
战士
奥杜姆。
苏亚维门特。
战士
阿加马根。
提图斯。
和尚
阿加马根。
亚洛克。
术士。
奥杜姆。
$(document.foundation();

我已经把你的代码中重要的部分写了出来,并编写了一个工作提琴

这是现在的搜索引擎:

    var searchText = $(this).val().toLowerCase();
    $.each($("#table tbody tr"), function() {
        if($(this).text().toLowerCase().indexOf(searchText) === -1)
           $(this).hide();
        else
           $(this).show();                
    });

您也可以将其用于这些事情;)

我发现上述解决方案在理论上是可行的(尽管它不起作用),但我发现这更有效:

$('#search-field').on('keyup', function(e) {
    if ('' != this.value) {
        var reg = new RegExp(this.value, 'i'); // case-insesitive

        $('.table tbody').find('tr').each(function() {
            var $me = $(this);
            if (!$me.children('td:first').text().match(reg)) {
                $me.hide();
            } else {
                $me.show();
            }
        });
    } else {
        $('.table tbody').find('tr').show();
    }
});
如果要搜索多个列,只需更改:

if (!$me.children('td:first').text().match(reg)) {
致:


您可以使用此代码。它工作得很好

$('#search').keydown(function () {
      var searchitem = $('#search').val();
      if (searchitem == '' || searchitem == null || searchitem == undefined)           {
          $('#table tbody tr').show();
      }
      else {
          searchitem = searchitem.toUpperCase();
          $('#table tbody tr').hide();
          $('#table tbody tr').each(function () {
              if ($(this).text().indexOf(searchitem) > -1) {
                  $(this).show();
              }
          });
      }
  });

对于我的桌子来说,张贴的方式有点慢。我想出了一个不同的解决方案,似乎要快得多

如果要搜索每个单元格,可以向单元格添加属性(我使用了数据名),例如:
John Smith
。然后可以使用以下javascript代码:

$("#search").keyup(function() {
  var val = this.value.trim().toLowerCase();
  if ('' != val) {
    var split = val.split(/\s+/);
    var selector = 'td';
    for(var i=0;i<split.length;i++){
      selector = selector+'[data-name*='+split[i]+']';
    }
    $('tr').hide();
    $(selector).closest('tr').show();
  } else {
    $('tr').show();
  }
});
$(“#搜索”).keyup(函数(){
var val=this.value.trim().toLowerCase();
如果(“”!=val){
var split=val.split(/\s+/);
var选择器='td';

对于(var i=0;i,我在项目中使用了这个精确的代码片段,以使表可搜索

<script>
    $(document).ready(function () {
        $("#input").on("keyup", function () { //here #input textbox id 
            var value = $(this).val().toLowerCase();
            $("#table tr").filter(function () { //here #table table body id 
                $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
            });
        });
    });
</script>

$(文档).ready(函数(){
$(“#输入”)。在(“keyup”上,函数(){//here#输入文本框id
var value=$(this.val().toLowerCase();
$(“#table tr”).filter(函数(){//here#表体id
$(this).toggle($(this).text().toLowerCase().indexOf(value)>-1)
});
});
});
别忘了加上这个

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

什么东西不起作用?首先,
$(“搜索”).keyup(…)
应该是
$(“#搜索”).keyup(…)
$(“搜索”).keyup(函数(){
替换为
$(“#搜索”)
$("#search").keyup(function() {
  var val = this.value.trim().toLowerCase();
  if ('' != val) {
    var split = val.split(/\s+/);
    var selector = 'tr';
    for(var i=0;i<split.length;i++){
      selector = selector+'[data-name*='+split[i]+']';
    }
    $('tr').hide();
    $(selector).show();
  } else {
    $('tr').show();
  }
});
<script>
    $(document).ready(function () {
        $("#input").on("keyup", function () { //here #input textbox id 
            var value = $(this).val().toLowerCase();
            $("#table tr").filter(function () { //here #table table body id 
                $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
            });
        });
    });
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>