Jquery 有固定回应的聊天室

Jquery 有固定回应的聊天室,jquery,json,ajax,html,Jquery,Json,Ajax,Html,我不确定是否已经解决了这个问题(我肯定可能已经解决了),但我在解决这个问题时遇到了一些问题,似乎无法准确找到我想要的东西。以下是我的聊天室: <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <style> body { font:16px Helvetica; color: #a8199f; text-align:center; padding:35px;

我不确定是否已经解决了这个问题(我肯定可能已经解决了),但我在解决这个问题时遇到了一些问题,似乎无法准确找到我想要的东西。以下是我的聊天室:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
<style>
body {
font:16px Helvetica;
color: #a8199f;
text-align:center;
padding:35px; }

form, p, span {
    margin:0;
    padding:0; }

input { 
    font:16px Helvetica; }

a {
    color:#0000FF;
    text-decoration:none; }

a:hover { 
    text-decoration:underline; }

#wrapper {
    margin:0 auto;
    padding-bottom:25px;
    background: #bdbbb7;
    width:50%;
    border:1px solid #ACD8F0; }

#chatbox {
    text-align:left;
    margin:0 auto;
    margin-bottom:25px;
    padding:10px;
    background:#fff;
    width:50%;
    border:1px solid #ACD8F0;
    overflow:auto; }

#chatbox p {
    padding:1em;
    margin:1em;
    background:#afd6ed;
    border:1px solid #BDBDBD;
    -moz-border-radius:4px;
}

#usermsg {
    width:50%;
    border:1px solid #ACD8F0; }

#submit { 
    width: 60px; }


.welcome { 
    float:left; }

.logout { 
    float:right; }

.msgln { 
    margin:0 0 2px 0; }
    </style>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js" ></script>
</head>
<body>
<div id="wrapper">
    <div id="menu">
        <p class="welcome">Welcome</p>
        <p class="logout"><a id="exit" href="#" onclick="window.close();">Exit Chat</a></p>
        <div style="clear:both"></div>
    </div>

    <div id="chatbox">
        <p>Here's our chat data</p>
        <div id="update"></div>
    </div>

    <form name="message">
        <input name="usermsg" type="text" id="usermsg" size="63" />
        <button type="button" id="submitmsg" value="send">Send</button>    
    </form>
</div> 

<script>

    $(document).ready(function(){
        $('#submitmsg').click(function(){
            var message = $('#usermsg').val();
            $('#chatbox').append('<p>' + message + '</p>');
            $('#usermsg').val('');
        });
    });

 function close_window(url){
        var newWindow = window.open('', '_self', ''); //open the current window
        window.close(url);
    };
</script>
<script src="script.js"></script>
</body>
</html> 
是对用户消息的响应。我尝试过这个脚本,但我知道我缺少了一些东西,似乎无法自己掌握它或找到合适的信息。如有任何帮助或推荐,将不胜感激。多谢各位

$('#usermsg').keyup(function(){
        var userResponse = $('#usermsg').val();
        $.getJSON('data.json', function(data){
            var output = '<div class="repsonse">';
            $.each(data, function(key, val){
                if(val.response != -1){
                    output += '<p>' + val.response + '</p>';
        });
        output += '</div>';
        $('#update').html(output);
    });
});
$('#usermsg').keyup(函数(){
var userResponse=$('#usermsg').val();
$.getJSON('data.json',函数(数据){
var输出=“”;
$。每个(数据、函数(键、值){
if(val.response!=-1){
输出+=''+val.response+'

'; }); 输出+=''; $('#update').html(输出); }); });
AJAX请求
getJSON
是异步的、非阻塞的。
函数(数据)
方法是回调,仅在
.getJSON()
请求成功完成时运行。而
$('#update').html(输出)
在您的示例中,它会立即运行—不会等待ajax请求完成

您需要将DOM操作移动到回调中:

$.getJSON('data.json', function(data) {
  var output = '<div class="repsonse">';
  $.each(data, function(key, val) {
      if (val.response != -1) {
        output += '<p>' + val.response + '</p>';
      }
  });//end each
  output += '</div>';
  $('#update').html(output);
});
$.getJSON('data.json',函数(数据){
var输出=“”;
$。每个(数据、函数(键、值){
if(val.response!=-1){
输出+=''+val.response+'

'; } })//结束每一个 输出+=''; $('#update').html(输出); });
好的,我现在明白了,但是我如何让它们作为对用户消息的响应出现?我想这可能是重复的,我不明白。我不知道如何使类似的东西与聊天盒和用户响应一起工作,从.json文件返回一个响应。
$.getJSON('data.json', function(data) {
  var output = '<div class="repsonse">';
  $.each(data, function(key, val) {
      if (val.response != -1) {
        output += '<p>' + val.response + '</p>';
      }
  });//end each
  output += '</div>';
  $('#update').html(output);
});