当您将鼠标悬停在表的某一行上时,会出现一个按钮。当我点击按钮时,我将如何用按钮编辑特定的行?目前,我正在使用css使按钮出现和消失:
button.editBtn{ visibility: hidden;}
tr:hover button.editBtn { visibility: visible;}
我的表格html代码是:
<div class="well">
<h2>Grocery List</h2>
<table class="table table-striped table-bordered table-condensed table-hover">
<thead>
<tr>
<th>Item</th>
<th>Quantity</th>
<th>Price</th>
</tr>
</thead>
<?php
$item_array;
$index = 0;
$index_2 = 1;
$r = "r";
$b="b";
foreach ($item_array as $id_array){ ?>
<tr id="r<?php echo $r.$index_2; ?>">
<td><?php echo $item_array[$index] ?></td>
<td> <?php echo $quantity_array[$index] ?></td>
<td>
<form method='POST' action='edit.php'>
<?php echo $price_array[$index];?>
<div id="editButtons">
<button type="button" id="e<?php echo $r.$index_2; ?>" style="align-content:right;" class="editBtn btn btn-sm btn-warning"><span class="glyphicon glyphicon-edit"></span></button>
<button type="button" id="d<?php echo $r.$index_2; ?>" style="align-content:right;" class="editBtn btn btn-sm btn-danger"><span class="glyphicon glyphicon-remove"></span></button>
<button type="button" id="s<?php echo $r.$index_2; ?>" style="align-content:right;" class="editBtn btn btn-sm btn-success"><span class="glyphicon glyphicon-circle-arrow-right"></span></button>
</div>
</form>
</td><?php
$index++;
$index_2++;
echo "</tr>";
} ?>
</table>
</div>
</div>
好吧,你会有一些工作。。。由于jQuery在你的问题标签上,我假设你正在使用它
为每个按钮设置不同的事件,因为按钮将执行不同的操作。
在删除按钮的Javascript事件中,我会隐藏将其淡出的行,然后通过AJAX调用PHP脚本来删除数据库寄存器,并在AJAX成功返回后删除HTML(调用jQuery remove方法)(当然,在执行所有这些操作之前,请用户确认)。
为了让编辑按钮工作,我看到了两个不错的选择:
- 把一个包含字段的表单放在表中,用正确的值填充它们,并让它们使用
display: none
,所有这些都是通过PHP实现的
或 - 单击编辑按钮,通过Javascript创建表单和字段并填充它们
就我个人而言,我更喜欢第一个选项,但如果你有一个真正大的表,那么动态创建表单和字段以减小HTML大小可能是个好主意。
无论如何,在单击编辑按钮时,您需要显示编辑字段的列(当前代码中不存在),隐藏按钮并显示另一个:确认按钮。
我认为你也可以使用禁用的编辑字段来显示数据,将其操作为正常文本,然后在单击编辑按钮时删除设计操作和禁用属性,但我从未使用过这种方法,所以我不确定这是否有效,或者任何浏览器中是否存在故障。
明白了吗?