jquery包装(动态元素)

jquery包装(动态元素),jquery,Jquery,所以我整个上午都在研究这个问题,我很确定代码是正确的,但是为了解释我试图在动态生成的输入中包含一个表,所以需要在其中包含tr/td。我已经检查了html/wrap/append等功能,但没有结果…:-( 看到评论那里的完整脚本,但实际上这是它的肉。任何帮助将不胜感激 我尝试了包装和文档写入,但它不喜欢。我错过了什么 $(document).ready(function() { $('#btnAdd').click(function() { var num = $(

所以我整个上午都在研究这个问题,我很确定代码是正确的,但是为了解释我试图在动态生成的输入中包含一个表,所以需要在其中包含tr/td。我已经检查了html/wrap/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>");
$(文档).ready(函数(){
$('#btnAdd')。单击(函数(){
var num=$('.clonedInput').length;//当前有多少个“可复制”输入字段
var newNum=newnumber(num+1);//正在添加的新输入字段的数字ID
//http://charlie.griefer.com/blog/2009/09/17/jquery-dynamically-adding-    形式元素/
//通过clone()创建新元素,并使用newNum值操纵其ID
var newElem=$('#input'+num).clone().attr('id','input'+newNum).wrap('');
//操纵新元素内输入的名称/id值
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('');
//在最后一个“可复制”输入字段后插入新元素
文件。填写(“”);
$('#input'+num).after(newElem);
文件。填写(“”);
$(document.ready()
中使用
document.write()
将删除现有文档并启动新文档。我很怀疑这是否是您的意图(尤其是因为您似乎试图将行添加到现有表中)


您需要使用jQuery函数将新元素插入DOM,而不是
document.write()
。有许多jQuery调用用于操作DOM。您可以看到其中一些调用。我猜您需要类似或的调用。

您能澄清您的问题吗?请查看
prepend()上的jQuery API
append()
-我认为这比
document.write()更舒适。