首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jquery包装(动态元素)

jquery包装(动态元素)
EN

Stack Overflow用户
提问于 2011-11-01 01:40:00
回答 1查看 425关注 0票数 0

所以我整个上午都在研究这一点--我很确定代码是正确的,但为了解释一下,我试图在动态生成的输入中包含一个表,所以需要tr/td。我已经检查了html/ reviewed /append等函数,但都没有用……:-(

查看完整脚本的注释,但实际上这就是它的要点。任何帮助都将不胜感激。

我尝试了包装和文档编写,但它不喜欢它。我错过了什么?

代码语言:javascript
复制
$(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>");
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-11-01 01:53:54

$(document).ready()中使用document.write()将清除现有文档并开始一个新文档。我很怀疑这是否是您想要的(特别是因为您似乎正在尝试向现有表中添加行)。

在向DOM中插入新元素时,需要使用jQuery函数,而不是document.write()。有许多用于操作DOM的jQuery调用。你可以看到其中的一些here。我猜你想要像append()after()这样的调用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7957427

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档