我正在使用 jquery 创建即时通讯程序,但在单击“发送”按钮后无法获取在消息字段中输入的消息
我有html
<body>
<div class="main-window">
<div class="chat-screen"></div>
<div class="bottom-wrapper">
<input class="text-bar"></input>
<input type="button" value="Send"class="send-btn">Send</input>
</div>
</div>
<body>
我尝试使用此 jquery 附加它
$('.send-btn').click(function() {
$(".text-bar").text().appendTo(".chat-screen");
});
但是好像不行。有人可以指出我正确的方向吗?
最佳答案
您需要.val()
改变
$(".text-bar").text()
至
$(".text-bar").val()
<小时/>
你的代码变成
Fiddle DEMO
$('.send-btn').click(function () {
$(".chat-screen").append($(".text-bar").val());
});
<小时/>
清除文本框并在新行中添加新聊天内容。
$(document).ready(function () {
var chat_screen = $(".chat-screen");
var text_bar = $(".text-bar")
$('.send-btn').click(function () {
chat_screen.append(text_bar.val() + '<br/>');
text_bar.val('');
});
});
关于javascript - 将文本值追加到 div onclick(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19813438/