jQuery 附加带有 div 值的输入文本字段

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

Append input text field with value of a div

jqueryappend

提问by jQuerybeast

I'm trying to append an input text field and its value to be the value of a div.

我正在尝试将输入文本字段和它的值附加为 div 的值。

Here is what I came up so far:

这是我到目前为止的想法:

$(this).append('<input type="text" value=' $('#div1').val() '>');

回答by Ry-

Don't use HTML strings for everything!

不要对所有内容都使用 HTML 字符串!

$(this).append(
    $('<input>', {
        type: 'text',
        val: $('#div1').text()
    })
);

回答by Royi Namir

$(this).append('<input type="text" value='+ $('#div1').html()+ '>');

回答by Kai Qing

$(this).append('<input type="text" value=' + $('#div1').val() + '>');

don't forget to concatonate with +

不要忘记与 + 连接

Also, this assumes $(this) is an actual object.

此外,这假定 $(this) 是一个实际对象。

回答by Haris Sultan

<div id="parent-dv">
    <lable for="posting">POST</lable>
    <textarea style="width:400px; height:auto;"></textarea>
    <input type="Submit" id="inputsubmit">
    <button id="clear">Clear</button>
</div>

$(document).ready(function(){
$('#inputsubmit').on('click',function(event)
 {
var $inpute2xt = $("#parent-dv").find('textarea').val();
$('#parent-dv').append("<p></p>").addClass("newp").append($inpute2xt);
   
 }
  );
 
 }
 );
 
 
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <style>
  </style>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
 
<div id="parent-dv">
 <lable for="posting">POST</lable>
 <textarea style="width:400px; height:auto;"></textarea>
 <input type="Submit" id="inputsubmit">
 <button id="clear">Clear</button>
 <p></p> 
 </div>
 

 
</body>
</html>