Live search through table rows

前端 未结 15 1809
失恋的感觉
失恋的感觉 2020-11-29 00:52

I want to do a live search through the table rows, using jQuery, the \"live\" word is the key, because I want to type the keywords in the text input, on the same site and I\

15条回答
  •  一整个雨季
    2020-11-29 01:16

    Using yckart's answer, I made it to search for the whole table - all td's.

    $("#search").keyup(function() {
        var value = this.value;
    
        $("table").find("tr").each(function(index) {
            if (index === 0) return;
    
            var if_td_has = false; //boolean value to track if td had the entered key
            $(this).find('td').each(function () {
                if_td_has = if_td_has || $(this).text().indexOf(value) !== -1; //Check if td's text matches key and then use OR to check it for all td's
            });
    
            $(this).toggle(if_td_has);
    
        });
    });
    

提交回复
热议问题