我研究了几个可编辑的表格插件,但我不确定它们是否真的满足我的需求。我想要做的就是捕获 td 元素上的点击,获取当前值,然后将 td html 更改为输入 + 按钮,按下按钮时,删除输入和按钮并将新文本放入 td 中。这是我所拥有的,但它根本不起作用。当您单击 td 时,整个表格就会消失并被输入替换。
这是 JS:
var selectedObj;
$('td').click(function(e){
// Handle the click
if ($('#currentInput').length == 0)
{
selectedObj = $(this);
var currentValue = $(this).text();
var newValue = currentValue;
$(this).html("<input id='currentInput' type='text' value='"+currentValue+"'/> <button onclick='setNewValue()'>Set</button>");
}
});
function setNewValue()
{
var newValue = $('#currentInput').val();
selectedObj.html('');
selectedObj.text(newValue);
}
和 HTML:
<p>
<table id='transitTable' border="0" cellspacing="2" cellpadding="2" class='display' width="400">
<tr id='1'>
<td class="etsHeader etsLeft">H1</td>
<td class="etsHeader etsLeft">H2</td>
<td class="etsHeader etsLeft">H3</td>
<td class="etsHeader etsLeft">H4</td></tr>
<tr id='2'>
<td class="etsValue etsOdd etsLeft">R1</td>
<td class="etsValue etsOdd etsLeft">R1</td>
<td class="etsValue etsOdd etsLeft">R1</td>
<td class="etsValue etsOdd etsLeft">R1</td></tr>
<tr id='3'>
<td class="etsValue etsEven etsLeft">R2</td>
<td class="etsValue etsEven etsLeft">R2</td>
<td class="etsValue etsEven etsLeft">R2</td>
<td class="etsValue etsEven etsLeft">R2</td></tr></table></p>
最佳答案
为什么不尝试 contentEditable = true,
检查一下here
关于jQuery 用户可编辑表格单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10659344/