所以我整个上午都在研究这一点--我很确定代码是正确的,但为了解释一下,我试图在动态生成的输入中包含一个表,所以需要tr/td。我已经检查了html/ reviewed /append等函数,但都没有用……:-(
查看完整脚本的注释,但实际上这就是它的要点。任何帮助都将不胜感激。
我尝试了包装和文档编写,但它不喜欢它。我错过了什么?
$(document).ready(function() {
$('#btnAdd').click(function() {
var num = $('.clonedInput').length; // how many "duplicatable" input fields we currently have
var newNum = new Number(num + 1); // the numeric ID of the new input field being added
//http://charlie.griefer.com/blog/2009/09/17/jquery-dynamically-adding- form-elements/
// create the new element via clone(), and manipulate it's ID using newNum value
var newElem = $('#input' + num).clone().attr('id', 'input' + newNum).wrap('<td />');
// manipulate the name/id values of the input inside the new element
newElem.children(':first').attr('id', 'name' + newNum).attr('name', 'name' + newNum).val('');
newElem.children(':second').attr('id', 'amt' + newNum).attr('name', 'amt' + newNum).val('');
newElem.children(':third').attr('id', 'value' + newNum).attr('name', 'value' + newNum).val('');
newElem.children(':fourth').attr('id', 'test' + newNum).attr('name', 'test' + newNum).val('');
// insert the new element after the last "duplicatable" input field
document.write("<tr>");
$('#input' + num).after(newElem);
document.write("</tr>");发布于 2011-11-01 01:53:54
在$(document).ready()中使用document.write()将清除现有文档并开始一个新文档。我很怀疑这是否是您想要的(特别是因为您似乎正在尝试向现有表中添加行)。
在向DOM中插入新元素时,需要使用jQuery函数,而不是document.write()。有许多用于操作DOM的jQuery调用。你可以看到其中的一些here。我猜你想要像append()或after()这样的调用。
https://stackoverflow.com/questions/7957427
复制相似问题