我开发一个可编辑的表格功能在以前的文章中所描述的数据属性的:Editable row in a table - is using the record ID on a <tr> element bad practice?靶向从它的父母<tr>与使用jQuery
我有以下标记:
<tr data-id="2">
<td>Foo</td>
<td><button class="editBtn">Edit</button></td>
</tr>
<tr data-id="3">
<td>Bar</td>
<td><button class="editBtn">Edit</button></td>
</tr>
使用jquery我已经能够通过将.editBtn
作为编辑目标,然后使用以下内容来计算出哪个是data-id
元素:
$('.editBtn').click(function() {
id = ($(this).closest('tr').data('id'));
console.log(id);
});
我不明白的是这个ID,那么我该如何定位td
这个元素,里面有我的类别名称(Foo和Bar,在这个例子中)?
我希望能够把内容在这些并将它们放置在一个可编辑字段这样的:
<td><input type="text" value="Foo"></td>
我还需要能够改变的“编辑”按钮,将“保存”按钮在这一点上,用不同的类名称,例如
<td><button class="saveBtn">Save</button></td>
我该怎么做?
使其显示:试试这个没有那么需要显示它的时候和隐藏editBtn –