javascript - 无法使用 jQuery 在 td 中获取输入?

标签 javascript jquery html search html-table

我无法获取<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();

DEMO

关于javascript - 无法使用 jQuery 在 td 中获取输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24955153/

相关文章:

javascript - HTML5 - 从本地路径动态创建图像?

javascript - 当验证失败时,如何防止 anchor 标记在 JavaScript 中重定向

javascript - Firebase 云函数 - 如何获取模型数据

html - 如何在html中并排对齐图像?

javascript - 如何使用ajax使用post并定位它的url

javascript - 如何将字符串转换为日期时间(格式为 D/MM/YYYY HH :MM:SS PM)

html - 不确定为什么链接是灰色的

javascript - jQuery 中的错误( undefined variable )

javascript - 网站上的 3D 图形非常酷,但这背后的技术是什么?

javascript - Jquery将html设置id附加到变量