我是Jquery/javascript.I的乞丐,我试图使用textarea的值来追加一个div .But,当我使用jquery追加(我想),但只显示变量name.Why时,会发生什么情况?
科德芬
$(document).ready(function() {
$('#textbox').click(function(event) {
if(event.which=13) {
if($('#enter').prop("checked")) {
$('#textbox').val('');
event.preventDefault();
}
}
$('#send').click(function() {
var userMessage = $('#textbox').val();
$('#textbox').val('');
$('#container').append("userMessage");
});
});
});* {
padding:0;
margin:0;
}
body {
background:#eee;
}
#container {
width:600px;
height:500px;
background:#fff;
border:1px solid black;
margin:0 auto;
}
#controls {
margin:0 auto;
width:600px;
}
#textbox {
resize:none;
width:540px;
}
#send {
width:50px;
height:30px;
margin-top: 0px;
position:absolute;
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container"></div>
<div id="controls">
<textarea id="textbox" placeholder="Enter your message here!"></textarea>
<button id="send">Send</button><br>
<input type="checkbox" id="enter"><br>
<label for="enter">Send on enter</label>
</div>
发布于 2015-08-19 19:28:34
$(document).ready(function() {
$('#textbox').keydown(function(event) {
if(event.which==13 && $('#enter').prop("checked")==true) {
$('#container').append("<br/>"+$('#textbox').val());
$('#textbox').val('');
event.preventDefault();
}
});
$('#send').click(function() {
var userMessage = $('#textbox').val();
$('#textbox').val('');
$('#container').append("<br/>"+userMessage);
});
});发布于 2015-08-19 19:23:28
在userMessage周围没有引号,所以不要使用append("userMessage"):
$('#container').append(userMessage);http://codepen.io/anon/pen/NqZGrm
https://stackoverflow.com/questions/32103912
复制相似问题