【发布时间】:2012-02-04 12:55:40
【问题描述】:
我正在尝试将输入文本字段及其值附加为 div 的值。
到目前为止,这是我想出的:
$(this).append('<input type="text" value=' $('#div1').val() '>');
【问题讨论】:
-
那么到底是什么问题?
我正在尝试将输入文本字段及其值附加为 div 的值。
到目前为止,这是我想出的:
$(this).append('<input type="text" value=' $('#div1').val() '>');
【问题讨论】:
不要对所有内容都使用 HTML 字符串!
$(this).append(
$('<input>', {
type: 'text',
val: $('#div1').text()
})
);
【讨论】:
$(this).append('<input type="text" value='+ $('#div1').html()+ '>');
【讨论】:
val() 值
$(this).append('<input type="text" value=' + $('#div1').val() + '>');
别忘了用 + 连接
此外,this 假设 $(this) 是一个实际对象。
【讨论】:
val() 值
<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>
【讨论】: