Javascript 克隆行时清除隐藏的输入
Im使用一个函数克隆html表格行和输入字段,以允许用户进行多个输入 我的html如下所示Javascript 克隆行时清除隐藏的输入,javascript,jquery,html,input,Javascript,Jquery,Html,Input,Im使用一个函数克隆html表格行和输入字段,以允许用户进行多个输入 我的html如下所示 <table id="ID_1"> <tr id="tr_id1"> <td><input type="hidden" value="databaseid"/></td> <td>Inputfield 1</td> <td>Inputfield 2 <
<table id="ID_1">
<tr id="tr_id1">
<td><input type="hidden" value="databaseid"/></td>
<td>Inputfield 1</td>
<td>Inputfield 2 </td>
</tr>
</table>
<button onclick="cloneRow('ID_1', 'tr_id1')"></button>
为了下一页的进一步工作,我只需要第一个
,因此通过复制,它应该只复制隐藏的输入,但没有边上的值,值应该像value=”“
如何解决这个问题?(可以使用jQuery)
Fiddle您可以使用以下jQuery代码清空行中输入的值:
$(row).find('input').val('')
在函数调用后使用上面的命令尝试以下操作:
function cloneRow(tablename,rowname) {
var value=$(this).prev('table').find('input:hidden').val(); // store the value in a variable
$(this).prev('table').find('input:hidden').val(''); // empty the input
var row = document.getElementById(rowname); // find row to copy
var table = document.getElementById(tablename); // find table to append to
var clone = row.cloneNode(true); // copy children too
clone.id = "newID"; // change id or other attributes/contents
table.appendChild(clone); // add new row to end of table
$(this).prev('table').find('input:hidden').val(value); // put the value in its place again
$('.pickDate').each(function() {
$(this).datepicker({ dateFormat: 'dd.mm.yy' });
});
}
更新:
使用jQuery的:
$('button').click(function(){
var clone=$(this).prev('table').find('tr:first').clone();
clone.attr('id','newID');
clone.find('input:hidden').val('');
$(this).prev('table').append(clone);
});
正确地缩进代码,如果可能的话,提供fiddle()@NitsanBaleli我只是负责缩进。。。另一方面,我看不出为什么这应该是一个没有小提琴的问题测试…@NitsanBaleli这在放置正确的位置后起了作用:)!谢谢
function cloneRow(tablename,rowname) {
var value=$(this).prev('table').find('input:hidden').val(); // store the value in a variable
$(this).prev('table').find('input:hidden').val(''); // empty the input
var row = document.getElementById(rowname); // find row to copy
var table = document.getElementById(tablename); // find table to append to
var clone = row.cloneNode(true); // copy children too
clone.id = "newID"; // change id or other attributes/contents
table.appendChild(clone); // add new row to end of table
$(this).prev('table').find('input:hidden').val(value); // put the value in its place again
$('.pickDate').each(function() {
$(this).datepicker({ dateFormat: 'dd.mm.yy' });
});
}
$('button').click(function(){
var clone=$(this).prev('table').find('tr:first').clone();
clone.attr('id','newID');
clone.find('input:hidden').val('');
$(this).prev('table').append(clone);
});