【发布时间】:2016-08-26 13:54:50
【问题描述】:
我想根据 ajax 请求的响应在文本区域显示一些文本。
<div id="wrapper">
<div id="menu">
<p class="welcome">Welcome, <b></b> <%=session.getAttribute( "username" )%> </p>
</div>
<!-- This div will contain the chatlog. -->
<div id="chatbox"></div>
<form name="message" action="" method="post">
<input name="usermsg" type="text" id="usermsg" size="63" />
<input name="submitmsg" type="submit" id="submitmsg" value="Send" />
</form>
</div>
我想在 ajax 请求响应中设置“聊天框”文本。 这是聊天框的css文档
#chatbox {
text-align:left;
margin:0 auto;
margin-bottom:25px;
padding:10px;
background:#fff;
height:270px;
width:430px;
border:1px solid #ACD8F0;
overflow:auto; }
这是ajax调用
function loadLog(){
$.ajax({
type: "GET",
url: "HandleMessage",
//contentType: "application/json",
/*data: {
card: JSON.stringify(card)
},*/
success: function(response) {
if(response != null)
{
$("#chatbox").attr("value", "aa"); // I want to concat current value and response here
}
//document.getElementById('chatbox').value = 'fff';
//alert(response);
},
error: function(data) {
alert('errorr');
}
});
尝试了很多东西,但没有奏效。
试过了,
$("#chatbox").attr("value", response);
$("#chatbox).val(response);
$(".chatbox").html(response);
【问题讨论】:
-
使用
$("#chatbox").html('whatever')但为什么它是textarea而不是div或任何其他正常元素? textareas 用于向服务器发送数据,而不是向用户显示内容。 -
有效!感谢您的帮助
-
除了获取当前值并将响应添加到其中之外,还有更简单的方法来连接响应吗?
-
我并没有真正明白......你在代码中的哪里连接它?
-
我需要$("#chatbox").html(stringvalue)的地方,stringvalue应该是text field的当前值+response