如何根据单元格值删除 html 中的行
How to delete row in html based on cell value
我正在尝试根据 table 的单元格值删除一行。
在我的 HTML 页面中,我有一个简单的搜索功能。它会通过 ajax 然后 PHP 将文本框中的值发送到数据库,然后 PHP 将根据数据库中的内容动态创建 table。
我已尝试检查 table 中的单元格是否有特定值,如果为真,则当前行将被删除,函数名称为 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 中 table 的创建:
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 等?
注意:我并不是要从数据库中删除该行。我只想在 Match
为 0%
.
时删除该行
编辑,外部 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();
});
}
}
目前我的 table 确实显示了所有正确的值,但它没有删除具有 0% 的行。
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();
}
});
}
我正在尝试根据 table 的单元格值删除一行。
在我的 HTML 页面中,我有一个简单的搜索功能。它会通过 ajax 然后 PHP 将文本框中的值发送到数据库,然后 PHP 将根据数据库中的内容动态创建 table。
我已尝试检查 table 中的单元格是否有特定值,如果为真,则当前行将被删除,函数名称为 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 中 table 的创建:
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 等?
注意:我并不是要从数据库中删除该行。我只想在 Match
为 0%
.
编辑,外部 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();
});
}
}
目前我的 table 确实显示了所有正确的值,但它没有删除具有 0% 的行。
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();
}
});
}