jQuery jquery中的.clone()方法不复制值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12035910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:13:43  来源:igfitidea点击:

.clone() method in jquery without copying value

jqueryhtml

提问by Carlos

Possible Duplicate:
Jquery clone of a textbox without the content

可能的重复:
没有内容的文本框的 Jquery 克隆

I use .clonein 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.

.clone在 jQuery 中使用。当我克隆 ul 时,它也在克隆它的值。我只想复制结构而不是值。当您在输入字段中键入一些值然后单击克隆时,它也会克隆输入值。

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);
    });
});

回答by VisioN

You can try the code below. By the way, if you use the last version of jQuery I'd suggest using onmethod instead of live(here instead of bodyyou can use any parent of aelement).

你可以试试下面的代码。顺便说一句,如果您使用最新版本的 jQuery,我建议您使用onmethod 而不是live(在这里body您可以使用任何a元素的父元素)。

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

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

演示:http : //jsfiddle.net/vhq5p/18/

回答by Sudhir Bastakoti

Do you mean:

你的意思是:

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

Demo: jsFiddle

演示:jsFiddle