weixin_33720186 2017-01-09 17:23 采纳率: 0%
浏览 7

带罐头回复的聊天框

I am not sure if this has been addressed or not (I am sure it probably has been), but I am having issues figuring it out and cannot seem to find exactly what I am looking for. Here is what I have for my chatbox:

<!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> 

I want to make this .json file:

[
  {
    "response":"Hello",
  },
  {
    "response":"How may I help you?",
  },
  {
    "response":"Let me look into that for you.",
  },
  {
    "response":"Can you give me more information about that?",
  },
  {
    "response":"Is there anything else I can help you with?",
  },
  {
    "response":"Thank you, Have a good day.",
  }
]

be the response for user messages. I have tried this script, but I know I am missing stuff and just cannot seem to grasp it by myself or find the appropriate information. Any help or references would be greatly appreciated. Thank you.

$('#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);
    });
});
  • 写回答

1条回答 默认 最新

  • weixin_33727510 2017-01-09 17:39
    关注

    The AJAX request getJSON is asynchronous and non-blocking. The function(data) method is a callback and only runs when the .getJSON() request has successfully completed. Whereas $('#update').html(output) in your example runs immediately - It doesn't wait for the ajax request to complete.

    You need to move the DOM manipulations into the callback:

    $.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);
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路