简体   繁体   English

如何使用 jquery 更改输入位置及其值

[英]how to change placement of input and its value with jquery

I'm using .html() to get inputs inside a div and place it in another element using .html() again the problem is I cant get it with its value.我正在使用.html()在 div 中获取输入并再次使用.html()将其放置在另一个元素中,问题是我无法获取它的值。

 $("#myButton").click(function(){ $(".element2").html($(".element1").html()); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js</script> <div class="element1"> <input type="text" placeholder="enter something then clone it!"/> </div> <div class="element2"></div> <button id="myButton">click to clone</button>

To copy one element to another, you can use .clone()要将一个元素复制到另一个元素,您可以使用.clone()

var copy = $(".element1 > *").clone()

this will clone all of the elements with.element1 - you can then.append()/.appendTo() these to element2.这将使用 .element1 克隆所有元素 - 然后您可以将这些元素.append()/.appendTo() 复制到 element2。

Updated snippet:更新片段:

 $("#myButton").click(function(){ $(".element1 > *").clone().appendTo(".element2"); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="element1"> <input type="text" placeholder="enter something then clone it!"/> </div> <div class="element2"></div> <button id="myButton">click to clone</button>

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

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