用于查找td元素中的文本无效的函数

时间:2014-11-02 18:56:34

标签: javascript jquery html html5

所有

我在SO上看到了几种关于如何在表格中找到用td元素编写的文本或html的方法。出于某种原因,他们似乎并不适合我。我显然做了一些非常错误的事情,但我无法弄清楚是什么。

编辑:问题是我的td中的html()始终显示为undefined。我似乎无法使用html(),text()等

获取文本(EG company0)

这是我的功能。 #searchbox是一种输入类型:文本

$(document).ready(function () {
$('#searchbox').change(function () {
    var searchText = $(this).val();
    $('.prospect_table tr').each(function () {
        var obj =  $(this).find('.propsect_td');
        if (typeof obj != 'undefined') {
            if (hideText(obj.html(), searchText))
                $(this).show();
            else
                $(this).hide();
        }
    });
});
});

function hideText(prospectName, text) {
    if (prospectName == 'undefined')
        return false;

    if (prospectName.toLowerCase().indexOf(text.toLowerCase()) >= 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

这是我的页面来源

<input type="text" name="txtSearch" id="searchbox" value="Begin typing here..." />

<table class="prospect_table">
<tr>
    <th>
        ProspectName
    </th>
    <th>
        Inactive
    </th>
    <th></th>
</tr>

<tr>
    <td class="prospect_td">
        Company0
    </td>
    <td>
        <input class="check-box" disabled="disabled" type="checkbox" />
    </td>
    <td>
        <a href="/CrmWeb/Company/Edit/0">Edit</a> |
        <a href="/CrmWeb/Company/Details/0">Details</a> |
        <a href="/CrmWeb/Company/Delete/0">Delete</a>
    </td>
</tr>
<tr>
    <td class="prospect_td">
        Company1
    </td>
    <td>
        <input class="check-box" disabled="disabled" type="checkbox" />
    </td>
    <td>
        <a href="/CrmWeb/Company/Edit/0">Edit</a> |
        <a href="/CrmWeb/Company/Details/0">Details</a> |
        <a href="/CrmWeb/Company/Delete/0">Delete</a>
    </td>
</tr>

等...

有关如何改进并使其有效的任何建议?也许我需要更多的id标签而不是类?

感谢您提供任何帮助或建议!

2 个答案:

答案 0 :(得分:1)

试试这个。将'.propsect_td'改为'.prospect_td','IndexOf'改为'indexOf'并用obj.length!= 0替换了typeof obj!='undefined'。

$(document).ready(function () {
$('#searchbox').change(function () {
    var searchText = $(this).val();
    $('.prospect_table tr').each(function () {
        debugger
        var obj =  $(this).find('.prospect_td');
        if (obj.length != 0) {
            if (hideText(obj.html(), searchText))
                $(this).show();
            else
                $(this).hide();
        }
    });
});
});

function hideText(prospectName, text) {
    debugger
    if (prospectName == 'undefined')
        return false;

    if (prospectName.toLowerCase().indexOf(text.toLowerCase()) >= 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

答案 1 :(得分:0)

$(document).ready(function () {
$('#searchbox').change(function () {
    var searchText = $(this).val();
    $('.prospect_table tr').each(function () {
        var obj =  $(this).find('.prospect_td');

        console.log(obj.text());
        if (typeof obj != 'undefined') {
            if (hideText(obj.text(), searchText))
                $(this).show();
            else
                $(this).hide();
        }
    });
});
});

function hideText(prospectName, text) {
    if (prospectName == 'undefined')
        return false;

    if (prospectName.toLowerCase().indexOf(text.toLowerCase()) >= 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

很少拼写错误...而不是html()我使用text()属性。小提琴:http://jsfiddle.net/bn1403nk/