jQuery清除附加文本框
代码如下:jQuery清除附加文本框,jquery,html-table,Jquery,Html Table,代码如下: function newRow(){ var sourceRow = $('table#listings tr.row').html(); $('table#listings > tbody:last').append('<tr>'+sourceRow+'</tr>'); return false; } 函数newRow(){ var sourceRow=$('table#listings tr.row').html(); $(
function newRow(){
var sourceRow = $('table#listings tr.row').html();
$('table#listings > tbody:last').append('<tr>'+sourceRow+'</tr>');
return false;
}
函数newRow(){
var sourceRow=$('table#listings tr.row').html();
$('table#listings>tbody:last').append(''+sourceRow+'');
返回false;
}
HTML:
代码可以工作,但是我如何自动清除新添加的行文本框?因为它复制上一行的内容。我试过:
var sourceRow = $('table#listings tr.row').html();
var x = $(sourceRow).('input').children().val(''); //problematic line
$('table#listings > tbody:last').append('<tr>'+sourceRow+'</tr>');
return false;
var sourceRow=$('table#listings tr.row').html();
var x=$(sourceRow)。('input').children().val('')//有问题的线路
$('table#listings>tbody:last').append(''+sourceRow+'');
返回false;
但是没有起作用
我想要一个输出:
<table id='listings'>
<tbody>
<tr>
<td><input type='text' name='entry[]' value='ENTRY1' />
</tr>
<tr>
<td><input type='text' name='entry[]' value='' />
</tr>
</tbody>
</table>
这可能有效:
$('table#listings > tbody:last > tr:last input').val('')
这可能会奏效:
$('table#listings > tbody:last > tr:last input').val('')
您的选择器出现了一些问题。。。然后是您对选择器所做的操作。:)
函数newRow(){
var sourceRow=$('table#listings tr').html();
var destRow=$(''+sourceRow+'');
destRow.find('input').val('');
$('table#listings>tbody:last').append(destRow);
返回false;
}
我把它做得更原子一些,这样更容易阅读
添加:请注意,您的示例中没有“row”类。您的选择器出现了一些问题。。。然后是您对选择器所做的操作。:)
函数newRow(){
var sourceRow=$('table#listings tr').html();
var destRow=$(''+sourceRow+'');
destRow.find('input').val('');
$('table#listings>tbody:last').append(destRow);
返回false;
}
我把它做得更原子一些,这样更容易阅读
添加:请注意,您的示例中没有“row”类。这是怎么回事 如果可以避免,则不应将HTML序列化为字符串。您想克隆元素 还将选择器缓存到表中。这意味着您只需要选择一次
function newRow(){
var table = $('table#listings'),
newRow = table.find('tr:first').clone();
newRow.find(':input').val('');
table.find('> tbody:last').append(newRow);
}
这个怎么样
如果可以避免,则不应将HTML序列化为字符串。您想克隆元素
还将选择器缓存到表中。这意味着您只需要选择一次
function newRow(){
var table = $('table#listings'),
newRow = table.find('tr:first').clone();
newRow.find(':input').val('');
table.find('> tbody:last').append(newRow);
}
比我的干净。不过,您需要删除“行”类选择器。@John谢谢,没有注意到这一点。将修复:)比我的更干净。不过,您需要删除“行”类选择器。@John谢谢,没有注意到这一点。将修复:)