javascript - 在 td 元素中查找文本的功能不起作用

标签 javascript jquery html

全部,

我在这里看到了几种关于如何找到写在表中 td 元素中的文本或 html 的方法。由于某种原因,他们似乎不适合我。显然我做错了一些事情,但我不知道是什么。

编辑:问题是我的 td 中的 html() 始终显示为未定义。我似乎无法使用 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 标签而不是类?

感谢您的帮助或建议!

最佳答案

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

$(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;
    }
}

关于javascript - 在 td 元素中查找文本的功能不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26703079/

相关文章:

javascript - 需要单击以编辑文本示例的帮助

javascript - 访问 Polymer 元素中 <content> 的内容

javascript - Python POST 请求失败

JavaScript 函数变量作用域问题

javascript - 当我向下滚动到它们的 div 时如何使侧面 ID 导航链接处于事件/悬停状态

javascript - 提示下载

javascript - 如何实现带有不允许文本在其下方流动的 float 图标的列表项?

javascript - VueJS : Passing data from root element to child components through router-view

php - 如何获取带有在选择框中选择的值的文本框

javascript - 将 div 最初向右滚动,然后在溢出 div 内向左滚动