无法在jQuery的td中获取输入?

奥斯曼

我有一个问题,没有得到<input>指定内部<td>通过jQuery,我希望一旦输入了“输入搜索”,就获取<tr>具有这些条目的条目。然后,当输入为空时,返回所有条目。

这是我的代码:

<table id="hosts">
    <tr>
        <th>First</th>
        <th>Second</th>
    </tr>
    <tr>
        <td id="host"><input type="text" id="inputhost" value="214215" size="16"></td>
        <td id="rand"><input type="text" id="inputrand" value="442" size="16"></td>
    </tr>
    <tr>
        <td id="host"><input type="text" id="inputhost" value="1252512" size="16"></td>
        <td id="rand"><input type="text" id="inputrand" value="556" size="16"></td>
    </tr>
    <tr>
        <td id="host"><input type="text" id="inputhost" value="2114" size="16"></td>
        <td id="rand"><input type="text" id="inputrand" value="4666" size="16"></td>
    </tr>
    <tr>
        <td id="host"><input type="text" id="inputhost" value="3245466" size="16"></td>
        <td id="rand"><input type="text" id="inputrand" value="22654" size="16"></td>
    </tr>
    <tr>
        <td id="host"><input type="text" id="inputhost" value="24588" size="16"></td>
        <td id="rand"><input type="text" id="inputrand" value="54877" size="16"></td>
    </tr>
</table>
<br />
<input type="text" id="search" placeholder="  live search"></input>

这是我的jQuery代码:

function removeHighlighting(highlightedElements) {
    highlightedElements.each(function () {
        var element = $(this);
        element.replaceWith(element.html());
    })
}

function addHighlighting(element, textToHighlight) {
    var text = element.text();
    var highlightedText = '<em>' + textToHighlight + '</em>';
    var newText = text.replace(textToHighlight, highlightedText);

    element.html(newText);
}

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

    removeHighlighting($("table tr em"));

    $("#hosts tr").each(function (index) {
        if (index !== 0) {
            $row = $(this);

            var $host = $row.find("#host input#inputhost");
            var $dest = $row.find("#rand input#inputrand");
            var host_id = $host.text();
            var dest_id = $dest.text();
            var hostIndex = host_id.indexOf(value);
            var destIndex = dest_id.indexOf(value);
            if ((hostIndex == -1) && (destIndex == -1)) {
                $row.hide();
            }
            else if ((hostIndex != -1) && (destIndex != -1)) {
                addHighlighting($host, value);
                addHighlighting($dest, value);
                $row.show();
            }
            else if (hostIndex != -1) {
                addHighlighting($host, value);
                $row.show();
            }
            else {
                addHighlighting($dest, value);
                $row.show();
            }
        }
    });
});
彼得·卡

ID重复!您实际上并不需要它们;删除它们,然后您的代码将是:

        var $host = $row.find("td:first input");
        var $dest = $row.find("td:eq(1) input");

更新

您还必须将代码更新为:

        var host_id = $host.val();
        var dest_id = $dest.val();

演示

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

jQuery无法从表中获取输入数据

来自分类Dev

在jQuery中获取TD的值

来自分类Dev

在表td中获取输入值

来自分类Dev

从 td 表中获取输入值

来自分类Dev

无法使用find函数获取jquery中隐藏输入的值

来自分类Dev

jQuery获取<td>中的下一个输入元素值

来自分类Dev

如何在jQuery中获取除输入类型=“ hidden”以外的td innerhtml值

来自分类Dev

jQuery - 从数组中获取元素并按相同顺序输入每个连续的 td

来自分类Dev

jQuery从邻近的TD获取输入名称数组键值

来自分类Dev

获取没有输入标签的Td内部HTML-Jquery

来自分类Dev

使用jQuery在输入标签下获取元素的td值

来自分类Dev

如何获取jQuery中每个TD的背景?

来自分类Dev

从表中的td中的表单获取输入值

来自分类Dev

jQuery无法获取元素上的输入值

来自分类Dev

jQuery无法从输入表单获取值

来自分类Dev

jQuery的click事件无法获取输入值

来自分类Dev

jQuery无法获取元素上的输入值

来自分类Dev

从for循环生成的td标签中获取输入标签的值

来自分类Dev

使用JavaScript获取td标签中输入字段的值

来自分类Dev

在同一个 <td> 中获取输入

来自分类Dev

使用jQuery将输入的值存储在TD中

来自分类Dev

jQuery通过td中的隐藏输入字段值选择tr

来自分类Dev

使用jQuery将输入的值存储在TD中

来自分类Dev

jQuery获取TD的ID

来自分类Dev

无法使用 jQuery 从下一个表单中获取输入值

来自分类Dev

JSP无法从输入中获取价值

来自分类Dev

无法通过JavaScript从输入中获取值

来自分类Dev

JS无法从输入中获取价值

来自分类Dev

无法从 Laravel 中的 li 获取输入