简体   繁体   English

li 标签未使用 javascript 使用 socketio 生成值

[英]li tag is not generating values with socketio using javascript

I am having difficulty to generate li tag with a javascript socket when I submit values to a text field and send nothing returned back and shown * no error .当我将值提交到文本字段并且没有返回任何内容并显示*没有错误时,我很难使用 javascript 套接字生成li标签。 The js rendered correctly.The application integrated with python/flask, javascript, and HTML to give the desired output. js渲染正确。应用程序与 python/flask、javascript 和 HTML 集成,以提供所需的 output。

The desired output所需的 output

  • it should take text input and generate to the user它应该接受文本输入并生成给用户

What I tried我试过的

        document.querySelector('#frm').onsubmit = () => {
                const message = document.querySelector('#txtMessage').value;
                socket.emit('submit chat', { 'message': message });
            };
    });

Take a look at the following code snippet from flask, javascript and HTML查看来自 flask、javascript 和 HTML 的以下代码片段

index.js index.js

document.addEventListener('DOMContentLoaded', () => {

    // Connect to websocket
    var socket = io.connect(location.protocol + '//' + document.domain + ':' + location.port);

    // When connected, configure textarea (listening)
    socket.on('connect', () => {

        // textarea should emit an  event
        document.querySelector('button').onclick = () => {
                const message = document.querySelector('#txtMessage').value;
                socket.emit('talk', { 'message': message });
            };
    });

    // When a new announce announced, add to the unordered list (trigger)
    socket.on('announce', data => {
        const li = document.createElement('li');
        li.innerHTML = `Eyasu Twitt: ${data.message}`;
        document.querySelector('#messages').append(li);
    });
});

from app.py来自 app.py

@app.route("/")
def index():
    return render_template("index.html")

@socketPlug.on("talk")
def chat(data):
    message = data["message"]
    emit("announce chat", {"message": message}, broadcast=True)

from index.html来自索引.html

        <ul id="messages">
        </ul>
        <hr>
    <form id="frm">
        <textarea id="txtMessage" type="text"></textarea>
        <button id="submit" type="submit">Send</button>
    </form>

Your page reloads on submit, that is why your page doesn't respond because it doesn't store it in server storage.您的页面在提交时重新加载,这就是您的页面没有响应的原因,因为它没有将其存储在服务器存储中。 To stop it from reloading you can add the type="button" to the button and add these two lines to the end of your js emit...要阻止它重新加载,您可以将type="button"添加到按钮并将这两行添加到 js 发出的末尾...

document.querySelector("#txtMessage").value = '';
return false;

Also I don't think your parenthesis are aligned in your js code.另外我不认为你的括号在你的 js 代码中是对齐的。

For reference look at my code at Exception in thread error when updating my dict containing a list of messages in flask-socketio作为参考,请在更新包含 flask-socketio 中消息列表的字典时查看我在线程错误中的异常代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM