javascript - 搜索字符串和整数表 javascript

标签 javascript html

当我搜索某些内容时,我再次遇到问题,它返回当我键入随机字母和数字时排序的第一行表,它只显示表的第一行。这似乎是错误的,我在标题上添加了排序。我该如何纠正这个问题?

HTML

     <table id="mytable">
           <thead>
             <tr>
             <th id="date_distribution">Date of Distribution</th>
             <th id="semi_total" class = 'text-center'>Semi Total</th>
             <th>Action</th>
            </tr>
                </thead>
            <tr>
            <th>November 30 2017</th>
            <th>₱20,175.10</th>
            </tr>
             <tr>
            <th>December 15 2017</th>
            <th>₱19,838.20</th>
            </tr>
    </table>
        <br />
        <input type="text" id="search" placeholder="  live search"></input>

这是排序和搜索的代码

function sortTable(f,n){
    var rows = $('#mytable tbody  tr').get();

    rows.sort(function(a, b) {

        var A = getVal(a);
        var B = getVal(b);

        if(A < B) {
            return -1*f;
        }
        if(A > B) {
            return 1*f;
        }
        return 0;
    });

    function getVal(elm){
        var v = $(elm).children('td').eq(n).text().toUpperCase();
        if($.isNumeric(v)){
            v = parseInt(v,10);
        }
        return v;
    }

    $.each(rows, function(index, row) {
        $('#mytable').children('tbody').append(row);
    });
}
var f_date_distribution = 1;
var f_semi_total = 1;
$("#date_distribution").click(function(){
    f_date_distribution *= -1;
    var n = $(this).prevAll().length;
    sortTable(f_date_distribution,n);
});
$("#semi_total").click(function(){
    f_semi_total *= -1;
    var n = $(this).prevAll().length;
    sortTable(f_semi_total,n);
});

$("#search").on("keyup", function() {
    var value = $(this).val();

    $('#mytable tbody  tr').each(function(index) {
        if (index !== 0) {

            $row = $(this);

            var id = $row.find("td:first").text();
            var id2 = $row.find("td:nth-child(2)").text();

            if (id.indexOf(value) !== 0 && id2.indexOf(value) !== 0) {
                $row.hide();
            }
            else {
                $row.show();
            }
        }
    });
});

最佳答案

由于输入不是预期的(意味着您可能会输入大写字母),因此您应该使用 toUpperCase()toLowerCase() 检查字母

$("#search").on("keyup", function(e) {
    var value = $(this).val();
    
    $('#mytable tbody  tr').each(function(index) {
		$row = $(this);
		
		var id = $row.find("th:first-child").text();
		var id2 = $row.find("th:last-child").text();
		if(id.toUpperCase().indexOf(value.toUpperCase()) > -1 || id.toLowerCase().indexOf(value.toLowerCase()) > -1 || 
		    id2.indexOf(value) > -1){
		    $row.show();	
		}
		else {
			$row.hide();
			return;
		}
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="mytable">
       <thead>
         <tr>
         <th id="date_distribution">Date of Distribution</th>
         <th id="semi_total" class = 'text-center'>Semi Total</th>
         <th>Action</th>
        </tr>
            </thead>
    <tbody>
      <tr>
        <th>November 30 2017</th>
        <th>₱20,175.10</th>
      </tr>
       <tr>
        <th>December 15 2017</th>
        <th>₱19,838.20</th>
      </tr>
    </tbody>
</table>
<br />
<input type="text" id="search" placeholder="  live search"></input>

关于javascript - 搜索字符串和整数表 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47626212/

相关文章:

javascript - 在服务器控件中使用 javascript

c# - 自定义客户端消息

javascript - 如何隐藏SVG中超出其下指定SVG的部分?

javascript - 如果我不知道它的 id,如何选择元素

javascript - 使用鼠标悬停调用函数。 (不同的选择)

html - 如何在 <li> 中垂直对齐图像中间?

javascript - svg - 获取重叠形状区域的路径坐标

javascript - Javascript 中的经典字数统计算法

javascript - 如何获取当前显示在屏幕上的第一个可见 DOM 元素?

javascript - 如何阻止页面在一定时间后刷新 javascript