简体   繁体   English

如何使用 jQuery 克隆输入

[英]How to clone input with jQuery

I try to clone input field, but all I can do is to grab the value.我尝试克隆输入字段,但我所能做的就是获取值。

Here is the jsFiddle这是jsFiddle

When I try to do $('#input').html() i got and empty string.当我尝试执行$('#input').html()我得到了空字符串。

I know that the answer is somewhere on the surface, but I can't find it.我知道答案就在表面的某个地方,但我找不到。 Please help.请帮忙。

EDIT I DO NOT NEED a value.编辑我不需要一个值。 I said - i can do it.我说——我能行。 I need to CLONE the input as HTML.我需要将输入克隆为 HTML。 What here is not understandable?这里有什么不明白的?
EDIT 2 I tried to clone an element and get html with this:编辑 2我试图克隆一个元素并用这个获取 html:

$('#text').clone().html()

but it returns an empty value too.但它也返回一个空值。 Although if I try to get a val:虽然如果我尝试获得一个 val:

$('#text').clone().val() 

It returns a value normally它通常返回一个值

I think that's what you're trying to do :我认为这就是你想要做的:

<input id='test' value='10201' />
<p id="input-text"> Hello </p>

Then in jQuery :然后在 jQuery 中:

var inputField = $('#test').clone();
$('#input-text').html(inputField);

As answered on this answer ... You can use Javascript to get the outerHTML.正如这个答案的回答......您可以使用Javascript来获取outerHTML。

var inputHTML = $('#input')[0].outerHTML;

Then you can use that variable wherever you need to on the page!然后,您可以在页面上需要的任何位置使用该变量!

Hope that helps!希望有帮助!

EDIT: You can do this purely using jQuery, again, an answer on the above linked question...编辑:您可以完全使用 jQuery 来做到这一点,同样,对上述链接问题的回答...

$.fn.outerHTML = function() {
    return $(this).clone().wrap('<div></div>').parent().html();
};

var inputHTML = $("#input").outerHTML());

关于什么

$('#input-text').html($('#test').clone().removeAttr('value'));

您正在寻找的方法是clone()

Using the .clone() method to the specific DOM element will extract the HTML(everything about it gets duplicated, raw) inherently.对特定 DOM 元素使用.clone()方法将固有地提取 HTML(关于它的所有内容都被复制,原始)。

 $("#test").clone().appendTo("#input-text").removeAttr('value'); $("input:eq(1)").attr("id", "test_02");
 input { margin-left: 3px; border: solid 1px #ACE; width: 3.05em; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <input id='test' value='10201' /> <p id="input-text">Hello</p>

The .attr() method was added only to raise awareness of the one pitfall of utilizing the .clone() method - that being, it duplicates the id of the DOM element which is not best practice or even allowed in standard HTML programming.添加.attr()方法只是为了提高人们对使用.clone()方法的一个陷阱的认识 - 即,它复制了 DOM 元素的id ,这不是最佳实践,甚至在标准 HTML 编程中也不是允许的。 The best pratice in this case, would probably be to create a unique class that you know, will only deal with that specific DOM element so it can be duplicated as you like.在这种情况下,最好的做法可能是创建一个您知道的唯一classclass只会处理该特定 DOM 元素,以便您可以随意复制它。

Additional Resources其他资源
» Fiddle Example »小提琴示例
» clone() API »克隆() API
» appendTo() API » appendTo() API
» Class VS ID »类 VS ID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM