如何获取HTML文本框值 [英] How to get HTML textbox value

查看:118
本文介绍了如何获取HTML文本框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在网站中实现聊天功能。我在这里使用提示来获取客户名称。它工作正常,但问题是提示显示每个页面加载弹出窗口。我想通过文本框而不是提示来获取客户端名称。我该怎么办?



我正在尝试这个代码



我尝试过的:



I am trying to implement chatting features in a website. I used here prompt to get client name. Its working properly but problem is that "prompt" shows popup on every page load. I want to get client name by textbox not by the prompt. What should I do?

I am trying this code

What I have tried:

<div class="container">
        <input type="text" id="message" />
        <input type="button" id="sendmessage" value="Send" /><br />
        <button onclick="myFunction()">Try it</button>
        <input type="hidden" id="displayname" />
        <ul id="discussion"></ul>
    </div><script type="text/javascript">
       function myFunction() {
            // Declare a proxy to reference the hub.
            var chat = $.connection.chatHub;
            // Create a function that the hub can call to broadcast messages.
            chat.client.broadcastMessage = function (name, message) {
                // Html encode display name and message.
                var encodedName = $('<div />').text(name).html();
                var encodedMsg = $('<div />').text(message).html();
                // Add the message to the page.
                $('#discussion').append('<strong>' + encodedName
                    + '</strong>:&nbsp;&nbsp;' + encodedMsg);
            };
            // Get the user name and store it to prepend to messages.
            $('#displayname').val(prompt('Enter your name:', ''));
            // Set initial focus to message input box.
            $('#message').focus();
            // Start the connection.
            $.connection.hub.start().done(function () {
                $('#sendmessage').click(function () {
                    // Call the Send method on the hub.
                    chat.server.send($('#displayname').val(), $('#message').val());
                    // Clear text box and reset focus for next comment.
                    $('#message').val('').focus();
                });
            });
        };
    </script>

推荐答案

.connection.chatHub;
//创建集线器可以调用广播消息的函数。
chat.client.broadcastMessage = function(name,message){
// Html编码显示名称和消息。
var encodedName =
.connection.chatHub; // Create a function that the hub can call to broadcast messages. chat.client.broadcastMessage = function (name, message) { // Html encode display name and message. var encodedName =


('< div />')。text(name).html();
var encodedMsg =
('<div />').text(name).html(); var encodedMsg =


('< div />')。text(message).html();
//将消息添加到页面。
('<div />').text(message).html(); // Add the message to the page.


这篇关于如何获取HTML文本框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆