【发布时间】:2018-07-03 04:40:06
【问题描述】:
这是我的 PHP:
class generate{
// PROJECT TABLE GENERATOR
public function genTable(){
global $data;
global $select;
global $pdo;
$result = $data->filterData();
echo '<table class="table">
<tbody>';
echo '<tr>
<th>heaer1</th>
<th>Header2</th>
<th>header3</th>
<th>header4</th>
<th>header5</th>
</tr>
<tr class="addForm">
<td>
<form action="parsers/parseData.php" method="post">
<label>Name</label>
<input type="text" id="Name">
<label>Purpose</label>
<input type="text" id="Purpose" >
</form>
</tr>';
foreach($result as $res){
echo '<tr data-id="' . $res['ID'] . '" ><td>' . $res['Name'] . '</td>
<td>' . $res['Purpose'] . '</td>
<td>' . $res['Owner'] . '</td>
<td>' . $res['Start'] . '</td>
<td>' . $res['End'] . '</td>
<td style="width:110px;"><button type="button" id="deleteBtn" class="btn btn-default" name="deleteBtn"><b class="glyphicon glyphicon-trash"></b></button>
<button type="button" id="editBtn" class="btn btn-default" name="editBtn"><b class="glyphicon glyphicon-pencil"></b></button>
</td>
</tr>
<tr data-id="' . $res['ID'] . '" class="hidden" id="editForm">
<td>
<form action="/" method="post">
<div class="form-group">
<input type="text" id="Name" placeholder="Name">
</div>
</td>
<td>
<div class="form-group">
<input type="text" id="Purpose" placeholder="Purpose">
</div>
</td>
<td>
';
$select->genSelect();
echo '
</td>
</form>
</tr>
';
}
echo '</form>
</tbody></table>';
}
jQuery...
$('button#editBtn').click( function(){
var id = $(this).parents('tr').data('id');
var Formid = $(this).closest('tr[id=editForm]').removeClass('hidden');
console.log(Formid);
});
jQuery 不太好用,我想要做的是选择 tr 与 id 的 editForm 等于 tr在它上方,这样当我点击表格中它旁边的编辑按钮时,一个表单会下拉到按钮所在行的下方……有可能吗?
我也试过这个,但它返回 undefined
$('button#editBtn').click( function(){
var id = $(this).parents('tr').data('id');
var attrId = $('tr#editForm').parents().data("id");
console.log(attrId); });
哦,还有这个……
$(document).ready(function(e) {
$('tr#editForm').toggle(); });
当我选择正确的标签时,希望我能够再次切换它
【问题讨论】:
-
如果您发布呈现的 html 而不是难看的 PHP 代码,会更容易提供帮助
-
考虑
.attr('data-id') -
您的 ID 为 editRow 的
tr位于foreach循环内,因此多行具有相同的 ID! ID 必须是唯一的。这绝对是个问题。
标签: php jquery html-table