jQuery 清除克隆对象中的文本字段值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6845058/
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 21:27:29  来源:igfitidea点击:

clear text field value in clone object

jqueryclone

提问by Gowri

I am having trouble to clear text field value created from jQuery clonefunction. I am just copying add_new_contentcontent and appending that one with last add_new_content element.it works good.But in case, clicking add_new_box after enter some text in the text fields means. it clones with input value.but i want fresh text field.

我无法清除从 jQuery克隆函数创建的文本字段值。我只是复制add_new_content内容并将该内容附加到最后一个 add_new_content 元素。效果很好。但是,在文本字段中输入一些文本后单击 add_new_box 意味着。它使用输入值进行克隆。但我想要新的文本字段。

Mark up

标记

<div class="add_new_content">
<div class="add_new_box">ADD NEW</div>
<input type="text" value="" />
</div>   

Script

脚本

$(document).ready(function(){

 $(".add_new_box").live('click',function(){

 $('.add_new_content:last').clone().appendTo('.add_new_content:last');

});

});

Working Example is Here

工作示例在这里

How can i do that.

我怎样才能做到这一点。

回答by Frédéric Hamidi

You can match the cloned <input>element with find()and reset its value with val():

您可以<input>使用find()匹配克隆元素并使用val()重置其值:

$('.add_new_content:last').clone()
                          .find("input:text").val("").end()
                          .appendTo('.add_new_content:last');

回答by DefyGravity

set the value right before, or after, you clone it.

在克隆它之前或之后设置该值。

$(document).ready(function(){

 $(".add_new_box").live('click',function(){

 var clone = $('.add_new_content:last').clone();
 clone.find("input").val("");
 clone.appendTo('.add_new_content:last');

});

});

回答by marcnewport

remove the value after you clone it eg.

克隆后删除该值,例如。

var $clone = $('.add_new_content:last').clone();
$clone.find('input').val('');
$clone.appendTo('.add_new_content:last');