javascript函数仅适用于表中的第一行

时间:2019-10-10 07:47:00

标签: javascript php

我建立了一个简单的php网站,您可以在其中过滤表格。在每个条目的末尾都有一个名为“详细信息”的链接,该链接将用户重定向到显示每个条目的详细信息的站点。我想根据用户输入的过滤器值来更改链接。

这是JavaScript:

function filterRows() {
    var input, filter, table, tr, td, cell, i, j, detailLink;
    input = document.getElementById("filterfeld");
    filter = input.value.toUpperCase();
    table = document.getElementById("ftable");
    tr = table.getElementsByTagName("tr");

        for (i = 1; i < tr.length; i++) {

            tr[i].style.display = "none";
            td = tr[i].getElementsByTagName("td");
            for (var j = 0; j < td.length; j++) {
                cell = tr[i].getElementsByTagName("td")[j];

                if (cell) {


                    if (cell.innerHTML.toUpperCase().indexOf(filter) > -1) {
                        tr[i].style.display = "";
                        detailLink = cell.innerHTML;
                        if(detailLink.indexOf("href=") && document.getElementById("details").getAttribute('href') != "http://google.at"){
                            var el = document.querySelector('.details');
                            console.log(el.getAttribute('href'));
                            el.setAttribute('href',"http://google.at");
                            console.log(el.getAttribute('href'));
                        }
                        break;
                    }
                }
            }
        }

}

html表格:

if ($result->num_rows > 0) {
        echo "<table align='center' id='ftable' class='ftable'><tr><th>ID</th><th>Name</th><<th>E-Mail</th><th>Telefon</th><th>Geburtsdatum</th><th>Straße</th><th>Details</th></tr>";
        // output data of each row
        while ($row = $result->fetch_assoc()) {

            echo "<td></td><tr><td>" . $row["id"] . "</td><td>" . $row["firstname"] ."&nbsp;". $row["lastname"] . "</td><td>" . $row["email"] . "</td>
                <td>" . $row["phone"] . "</td><td>" . $row["birthdate"] . "</td><td>" . $row["street"] . "</td>
                <td><a href='singleentry.php?id=" .$row["id"]."' class='details' id='details'>Details</a></td></tr>";
        }
        echo "</table>";
    } else {
        echo "0 results";
    }

我已经填写了示例链接google.at。 通常,它应该更改我当前表ftable中的所有链接。 问题在于它仅更改表中的第一个链接,然后停止。

感谢任何帮助!

0 个答案:

没有答案