简体   繁体   English

在(禁用)输入字段上的搜索功能

[英]Search function on (disabled)input fields

I have a simple search function that I use on a table of information, but since I turned the text in the table into editable fields the search function isn't working anymore. 我有一个用于信息表的简单搜索功能,但是由于我已将表中的文本转换为可编辑字段,因此搜索功能不再起作用。

I tried to troubleshoot it in a few different ways, but can't seem to get it working. 我试图以几种不同的方式对它进行故障排除,但似乎无法使其正常工作。

Here's what I have got so far: 到目前为止,这是我得到的:

var $rows = $('.list #data');
$('#search').keyup(function() {
    var val = $.trim($(this).val()).replace(/ +/g, ' ').toLowerCase();

$rows.show().filter(function() {
    var text = $(this).text().replace(/\s+/g, ' ').toLowerCase();
    return !~text.indexOf(val);
}).hide();
});

This works just fine on simple plain text tables. 这在简单的纯文本表上很好用。 Here's a JSFiddle that shows you what I am trying to accomplish: 这是一个JSFiddle,它向您展示了我要完成的工作:

https://jsfiddle.net/je9mc9jp/8/ https://jsfiddle.net/je9mc9jp/8/

//SEARCH
var $rows = $('.list input');
$('#search').keyup(function() {
    var val = $.trim($(this).val()).replace(/ +/g, ' ').toLowerCase();

    $rows.show().filter(function() {
        var text = $(this).val().replace(/\s+/g, ' ').toLowerCase();
        return !~text.indexOf(val);
    }).hide();
});

Went a little deeper on this issue with a friend of mine and we managed to get what we wanted working. 与我的一个朋友对这个问题进行了更深入的探讨,我们设法得到了我们想要的工作。

Bob's answer was alright, but the problem was it wouldn't return the whole row of results. 鲍勃的回答很好,但是问题是它不会返回整行结果。 Just the specific term you searched for! 只是您搜索的特定字词!

Here's what worked for me: 这对我有用:

var $rows = $('.list .edit');
$('#search').keyup(function() {
    var val = $.trim($(this).val()).replace(/ +/g, ' ').toLowerCase();
        $rows.parents("tr").hide()
    var x = $rows.show().filter(function() {
        var text = $(this).val().replace(/\s+/g, ' ').toLowerCase();

        if(text.indexOf(val) > -1)
            return $(this);
    });     

    $(x).each(function(){
        $(this).parents("tr").show();
    });
});

JSFiddle demo JSFiddle演示

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

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