.clone() method in jquery without copying value [d

2019-04-20 00:47发布

问题:

Possible Duplicate:
Jquery clone of a textbox without the content

I use .clone in jQuery. When I clone ul it is cloning its value also. I only want to copy structure not value. When you type some value in input field and then click clone it will clone input value too.

Here is my code snippet:

<div class="str">
    <ul><li><input type="text" /></li></ul>
</div>
<a href="#">clone</a>

$(function() {
    $('a').live('click', function() {
        var cln = $('ul:last').clone();
        $('.str').append(cln);
    });
});

回答1:

You can try the code below. By the way, if you use the last version of jQuery I'd suggest using on method instead of live (here instead of body you can use any parent of a element).

$("body").on("click", "a", function() {
    $("ul:last").clone().appendTo(".str").find("input[type='text']").val("");
});​

DEMO: http://jsfiddle.net/vhq5p/18/



回答2:

Do you mean:

var cln= $("ul:last").clone().find("input").val("").end();

Demo: jsFiddle



标签: jquery html