[英]jQuery can't clone() and appendTo() with html before
People am here because am having this code that actually doesnt work, it bassically duplicate a row and make a new row for a new entry. 人们在这里是因为拥有此代码实际上不起作用,它大声地复制了一行并为新条目创建了新行。 Am ussing
clone()
and appendTo()
. 在使用
clone()
和appendTo()
。
But if you remove the last part of the html it will work, it seems like if there is a new tag it goes worng syntax, please could someone help me with this trouble?. 但是,如果您删除了html的最后一部分,它将可以正常工作,似乎如果有一个新标签使用了语法,请有人帮我解决这个麻烦吗?
<table>
<tr>
<td>Hello world!</td>
</tr>
</table>
Thanks! 谢谢!
There are few issues in your fiddle 你的小提琴里没什么问题
1: the row element selected to be cloned is wrong. 1:选择要克隆的行元素错误。
2: parent element should be table but that is wrongly selected. 2:父元素应为表格,但选择错误。
You can use the below modified code for cloning and appending the row. 您可以使用以下修改后的代码克隆和附加行。
$("#clone").click(function() {
i++;
$("#remove").removeAttr("disabled");
var parent = $("#data");
var tr = $("#data tr:last");
console.log(tr);
var e = tr.clone().appendTo(parent);
$(e).find("[type=text],[type=hidden]").each(function() {
$(this).val('');
});
});
updated Fiddle : https://jsfiddle.net/363m6dsy/6/ 更新的小提琴: https : //jsfiddle.net/363m6dsy/6/
Change the selector and use .after()
to append 更改选择器并使用
.after()
追加
var tr = $("#parent:last");
var e = $("#parent").clone();
$(tr).after(e);
FULL JS 全JS
$(document).ready(function() {
var i = 1;
$("#clone").click(function() {
i++;
$("#remove").removeAttr("disabled");
var tr = $("#parent:last");
var e = $("#parent").clone();
$(tr).after(e);
console.log(e);
$(e).find("[type=text],[type=hidden]").each(function() {
$(this).val('');
});
});
$("#remove").click(function() {
var tr = $("#parent:last-child");
if (i > 1) {
i--;
tr.remove();
}
if (i == 1) {
$("#remove").attr("disabled", "true");
}
});
});
Working fiddle . 工作提琴 。
Just clone row $("#parent").clone()
then append it $('#data').append(e);
只需克隆
$("#parent").clone()
然后附加$('#data').append(e);
like : 喜欢 :
$("#clone").click(function() {
i++;
$("#remove").removeAttr("disabled");
var e = $("#parent").clone();
$(e).find("[type=text],[type=hidden]").each(function() {
$(this).val('');
});
$('#data').append(e);
});
Hope this helps. 希望这可以帮助。
$(document).ready(function() { var i = 1; $("#clone").click(function() { i++; $("#remove").removeAttr("disabled"); var e = $("#parent").clone(); $(e).find("[type=text],[type=hidden]").each(function() { $(this).val(''); }); $('#data').append(e); }); $("#remove").click(function() { var tr = $("#parent:last-child"); if (i > 1) { i--; tr.remove(); } if (i == 1) { $("#remove").attr("disabled", "true"); } }); });
body { width: 700px; } table { width: 100%; } input[type="text"] { width: 130px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id="data" name="data"> <tr> <td colspan="9"> <input type="button" value="Add" name="clone" id="clone"> <input type="button" value="Remove" name="remove" id="remove"> </td> </tr> <tr align="center"> <td rowspan="2">N°</td> <td rowspan="2">Activity</td> <td rowspan="2">Field1</td> <td rowspan="2">Resources</td> <td colspan="2">Dates</td> <td rowspan="2">Field2</td> <td rowspan="2">Date 1</td> <td rowspan="2">Status</td> </tr> <tr align="center"> <td>Start Date</td> <td>End Date</td> </tr> <tr name="parent" id="parent"> <td align="center"> <input id="val[]" name="val[]" type="text" value="1" required> <input id="val[]" name="val[]" type="hidden" value="13256"> </td> <td align="center"> <input id="val[]" name="val[]" type="text" value="2" required> </td> <td align="center"> <input id="val[]" name="val[]" type="text" value="3" required> </td> <td align="center"> <input id="val[]" name="val[]" type="text" value="4" required> </td> <td align="center"> <input id="val[]" name="val[]" type="text" value="5" required> </td> <td align="center"> <input id="val[]" name="val[]" type="text" value="6" required> </td> <td align="center"> <input id="val[]" name="val[]" type="text" value="7" required> </td> <td align="center"> <input id="val[]" name="val[]" type="text" value="8" required> </td> <td align="center"> <input id="val[]" name="val[]" type="text" value="9" required> </td> </tr> </table> <table> <tr> <td>Hello world!</td> </tr> </table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.