Append input text field with value of a div

2020-08-23 04:57发布

问题:

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

Here is what I came up so far:

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

回答1:

Don't use HTML strings for everything!

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


回答2:

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


回答3:

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

don't forget to concatonate with +

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



回答4:

<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>



标签: jquery append