【问题标题】:How to delete row in html based on cell value如何根据单元格值删除html中的行
【发布时间】:2015-06-22 00:32:39
【问题描述】:

我正在尝试根据表格的单元格值删除一行。

在我的 HTML 页面中,我有一个简单的搜索功能。它将文本框中的值通过 ajax 然后 PHP 发送到数据库,然后 PHP 将根据数据库中的内容动态创建一个表。

我试图检查表格中的单元格是否有特定值,如果它是真的,那么当前行将被删除,函数名称是checkMatch()。但是它不起作用。

这是 HTML 中的按钮,HTML 的其余部分只是一个文本框和下拉框:

 <input type="button" id="device_search" value="Search" onclick="searchDevice(); checkMatch();"></input>

这是外部 JavaScript 文件中的函数:

function checkMatch()
{
    var row = document.getElementById("thisRow");
    var cell = Row.getElementById("match");
    if(cell[0].innerText = "0%")
    {
        row.deleteRow(this);
    }
}

下面是在 PHP 中创建表:

if($num_row)
{
    echo "<table id=\"myTable\" border=\"1\">";
    echo "<tr>
    <th>Board ID</th>
    <th>Tester Name</th>
    <th>Board Name</th>
    <th>Current Slot</th>
    <th>Log Created</th>
    <th>Required Slot</th>
    <th>Match</th>
    </tr>";

    while($row = mysql_fetch_assoc($result))
    {
        $board_id = $row['board_id'];
        $tester_name = $row['tester_name'];
        $board_name = $row['board_name'];
        $config = $row['config'];
        $log_created = $row['log_created'];
        $req_config = $row['configuration'];

        $exploded = explode(",", $req_config);
        $count =  count($exploded);

        $j = 0;

        foreach($exploded as $value)
        {
            $number = strlen($value);
            $arr = str_split($value, $number);

            if(in_array($config, $arr))
            {
                $j += 1;
                $j /= ($count / 100);
                echo $j;
            }
            else
            {

            }
        }

        echo "<tr id = \"thisRow\">
                    <td>$board_id</td>
                    <td>$tester_name</td>
                    <td>$board_name</td>
                    <td>$config</td>
                    <td>$log_created</td>
                    <td>$req_config</td>
                    <td id = \"match\">$j%</td>
                 </tr>";
    }
    echo "</table>";
}

我正在检查的列是最后一列,即Match 列。我的想法是只要Match 的单元格值为0%,就删除当前行。我似乎找不到我的代码有什么问题,我也不擅长操作 DOM 元素,所以一定有一些错误。对此有什么帮助吗?还是有其他方法,通过 PHP 等?

注意:我不是要从数据库中删除该行。我只想在Match0% 时删除该行。

编辑,外部 js 文件中searchDevice() 的代码:

function searchDevice()
{
    var device_name = $("#device_name").val();
    var tester_type = $("#tester_type").val();
    var page = "database.php";

    if(device_name==""||tester_type=="")
    {
        alert("Please do not leave any blanks.");
    }
    else
    {
        $.post(page, {
            device_name : device_name,
            tester_type : tester_type,
            action : "search"
            }, function(data) {
            $("div#display_board").html(data);
            checkMatch();
        });
    }
}

目前我的表格确实显示了所有正确的值,但它不会删除 0% 的行。

【问题讨论】:

  • 如果searchDevice()是一个ajax调用,当checkMatch()被执行时,还没有返回搜索结果。您需要在searchDevice() 回调中执行checkMatch()
  • @Neverever 按照您的建议尝试过,同样的问题。
  • 请提供searchDevice()的邮政编码,同时行ID应该是唯一的id="thisRow"

标签: javascript php jquery html row


【解决方案1】:
  • checkMatch() 应该在 ajax 成功回调时调用
  • id 在 HTML 文档中应该是唯一的

PHP

    // use `class` instead of `id`
    echo "<tr class=\"thisRow\">
                <td>$board_id</td>
                <td>$tester_name</td>
                <td>$board_name</td>
                <td>$config</td>
                <td>$log_created</td>
                <td>$req_config</td>
                <td class=\"match\">$j%</td>
             </tr>";

Javascript

function checkMatch()
{
    $("#myTable tr.thisRow").each(function() {
        var thisRow = $(this);
        var match = thisRow.find(".match");

        // note the `==` operator
        if(match.text() == "0%") {
            thisRow.hide(); 
            // OR thisRow.remove();
        }
    });
}

【讨论】:

  • 啊,是的。谢谢你。 Up 也投票支持快速简单的回答和解释。 :)
猜你喜欢
  • 1970-01-01
  • 2017-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-13
相关资源
最近更新 更多