Js聊天控制器

时间:2017-01-11 07:29:27

标签: javascript jquery html

我是Javascript(纯粹的javascript)的新手,我正在努力创建一个聊天控制器应用程序来执行以下操作。

  1. 用户输入内容。
  2. 我的知识库发布了一个帖子。
  3. 服务器响应消息。
  4. 目前,我点击按钮后无法执行此帖子块。

    以下是我的代码。

    <html>
    <head>
    <meta charset="utf-8" />
    <meta name="viewport"
        content="width=device-width, initial-scale=1, maximum-scale=1" />
    <meta name="description" content="" />
    <meta name="author" content="" />
    
    <link rel="stylesheet"
        href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script
        src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script
        src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    
    <!--[if IE]>
            <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
            <![endif]-->
    <title>Bootstrap Chat Box Example</title>
    <!-- BOOTSTRAP CORE STYLE CSS -->
    <link href="assets/css/bootstrap.css" rel="stylesheet" />
    <!-- FONT AWESOME  CSS -->
    <link href="assets/css/font-awesome.css" rel="stylesheet" />
    <!-- CUSTOM STYLE CSS -->
    <link href="assets/css/style.css" rel="stylesheet" />
    
    <script type="text/javascript" src="assets/js/jquery.min.js"></script>
    <script type="text/javascript">
        // jQuery Document
    
        $(document).keypress(function(e) {
            if (e.which == 13) {
                $("#submitmsg").click();
            }
        });
    
        $(document).ready(function() {
            $('#submitmsg').bind('click', function() {
                var message = $('#usermsg').val();
                $('#chatbox').append('<p>' + message + '</p>');
                $('#usermsg').val('');
                //alert(message);
    
            });
        });
    
        function serverResponse(message) {
            $('#chatbox').append('<p>Server: ' + message + '</p>');
        }
    
        $("#submitmsg")
                .click(
                        function() {
                            alert("Hi");
                            var inputtedText = $("#usermsg").val();
                            var params = {};
    
                            var controllerUrl = 'https://westus.api.cognitive.microsoft.com/qnamaker/v1.0/knowledgebases/bde3c190-58bd-40d8-9ff1-c35eb18588be/generateAnswer';
                            $.ajax({
                                url : controllerUrl,
                                type : 'POST',
                                data : {
                                    "question" : inputtedText
                                },
                                beforeSend : function(xhrObj) {
                                    // Request headers
                                    xhrObj.setRequestHeader(
                                            "Ocp-Apim-Subscription-Key", "MyKey");
                                    xhrObj.setRequestHeader('Content-Type',
                                            'application/json; charset=UTF-8');
    
                                },
                                success : function(data) {
                                    var dataObj = data;
                                    alert('Data:' + JSON.stringify(dataObj));
                                    $('#resultvalue').html(data);
                                    serverResponse(JSON.stringify(dataObj));
                                    alert(data);
                                }
                            });
                        });
    </script>
    
    
    </head>
    <body>
    
        <nav class="menu">
            <div class="container-fluid">
                <div class="navbar-header">
                    <div class="back">
                        <img src="acn.jpg" draggable="false" />
                    </div>
                    <div class="name">Accenture Compliance</div>
                </div>
                <ul class="nav navbar-nav navbar-right">
                    <li><a href="#"><span class="glyphicon glyphicon-user"></span>
                            User Name</a></li>
                    <li><a href="#"><span class="glyphicon glyphicon-log-out"></span>
                            Logout</a></li>
                </ul>
            </div>
        </nav>
        <br />
        <br />
        <br />
        <div class="container">
            <div class="row pad-top pad-bottom">
                <div class="col-lg-3 col-md-3 col-sm-3">
                    <div class="chat-box-new-div">
                        <div class="chat-box-new-head">Compliance Avatar</div>
                        <div class="panel-body chat-box-new">
    
                            <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br />
                            <br /> <br /> <br /> <br />
    
                        </div>
                    </div>
                </div>
                <div class=" col-lg-6 col-md-6 col-sm-6">
                    <div class="chat-box-div">
                        <div class="chat-box-head">
                            CHAT HISTORY
                            <div class="btn-group pull-right">
                                <button type="button" class="btn btn-info dropdown-toggle"
                                    data-toggle="dropdown" aria-expanded="false">
                                    <span class="fa fa-cogs"></span> <span class="sr-only">Toggle
                                        Dropdown</span>
                                </button>
                                <ul class="dropdown-menu" role="menu">
                                </ul>
                            </div>
                        </div>
                        <div class="panel-body chat-box-main">
                            <div class="chat-box-left">Hello, Welcome!. You can ask me
                                questions on Compliance Policy ..</div>
                            <div class="chat-box-name-left">
                                <img src="compiler-bot-static.gif"
                                    alt="bootstrap Chat box user image" class="img-circle" /> - Bot
                            </div>
                            <hr class="hr-clas" />
                            <div class="chat-box-right" id="chatbox"></div>
                            <div class="chat-box-name-right">
                                <img src="smiley.jpg" alt="bootstrap Chat box user image"
                                    class="img-circle" /> - You
                            </div>
    
                        </div>
                        <div class="chat-box-footer">
                            <div class="input-group">
                                <input type="text" class="form-control"
                                    placeholder="Enter Text Here..." id="usermsg"> <span
                                    class="input-group-btn">
                                    <button class="btn btn-info" type="button" id="submitmsg">SEND</button>
                                </span>
                            </div>
                        </div>
    
                    </div>
    
                </div>
                <div class="col-lg-3 col-md-3 col-sm-3">
                    <div class="chat-box-new-div">
                        <div class="chat-box-new-head">Frequently Asked Questions ..
                        </div>
                        <div class="panel-body chat-box-new">
                            <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br />
                            <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br />
                            <br /> <br /> <br />
                        </div>
                    </div>
                </div>
            </div>
    
            <!-- USING SCRIPTS BELOW TO REDUCE THE LOAD TIME -->
            <!-- CORE JQUERY SCRIPTS FILE -->
            <script src="assets/js/jquery-1.11.1.js"></script>
            <!-- CORE BOOTSTRAP SCRIPTS  FILE -->
            <script src="assets/js/bootstrap.js"></script>
    </body>
    
    </html>
    

    为了检查函数是否实际被调用,我在("#submitmsg").click(function() {...内添加了一个警告,令我惊讶的是,它没有提醒任何事情,这意味着没有被调用。

    请让我知道我哪里出错了,我该如何解决这个问题。

    由于

1 个答案:

答案 0 :(得分:1)

您的问题是由添加点击事件监听器的时间引起的。

在文档触发就绪事件之前,尚未加载DOM元素。

由于这个原因,js(jquery选择器)找不到&#34; submitmsg&#34;用于添加事件侦听器的元素

检查以下链接&amp;代码

https://jsfiddle.net/f6fv1xvw/

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript">
    // maybe alerted [length = 0] because dom element loaded not yet
    alert("before ready : " + $("#submitmsg").length);

    $(document).ready(function() {
        // maybe alerted [length = 1] because dom element loaded completly
        alert("after ready : " + $("#submitmsg").length);

        // need to add event listener this timing(after ready)
        $("#submitmsg").click(function() {
        alert("Hi");
    });
  });
</script>

然后再次检查你的源码为什么加载jquery 3次。

// called 1 time in head
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

// called 2 time before body
<script type="text/javascript" src="assets/js/jquery.min.js"></script>

// called 3 time in body    
<script src="assets/js/jquery-1.11.1.js"></script>