克隆输入但使用Jquery的空值

时间:2011-04-24 10:15:15

标签: jquery html forms

我有大量的HTML:

<div class="bundle" id="bun">
        <div>
            <label for="title">title:</label><input name="name" id="name" value="led zeppelin III" type="text">
        </div>
        <div>
            <label for="content">content:</label><textarea type="textarea" cols="100" rows="3" name="content" id="content">composed largely at a remote cottage in wales known as Bron-Yr-Aur, this work represented a maturing of the band's music towards a greater emphasis on folk and acoustic sounds. this surprised many fans and critics, and upon its release the album received rather indifferent reviews</textarea>
        </div>
        <div>
        <label for="url">url for image:</label><input name="url" id="url" value="http://www.bob.co.il/wp-content/themes/CMOTA-1.0/demo-images/3.jpg" type="text">
        </div>
        <img src="http://www.bob.co.il/wp-content/themes/CMOTA-1.0/demo-images/3.jpg" alt="led zeppelin III" height="272">
    </div>

我正在使用clone()jquery方法将此批量复制到表单的末尾:

var newElem = $('#bun' + val).clone().attr('id', 'bun' + newval);
        newElem.children('input:first').attr('name', 'title' + newval).attr('id', 'title' + newval);
        newElem.children('textarea:first').attr('name', 'content' + newval).attr('id', 'content' + newval);
        newElem.children('input:nth-child(2)').attr('name', 'url' + newval).attr('id', 'url' + newval);
        $('#bun' + val).after(newElem);

它确实有效但我想只克隆“bun”元素的内容而没有输入的值和图像的src。我该怎么办呢?

感谢。

1 个答案:

答案 0 :(得分:3)

var newElem = $('#bun' + val).clone().attr('id', 'bun' + newval);
        newElem.children('input:first').attr('name', 'title' + newval).attr('id', 'title' + newval);
        newElem.children('textarea:first').attr('name', 'content' + newval).attr('id', 'content' + newval);
        newElem.children('input:nth-child(2)').attr('name', 'url' + newval).attr('id', 'url' + newval);
        $('#bun' + val).after(newElem);
        newElem.find('input,textarea').val('');