SignalR实时更新TextArea [英] SignalR Updating TextArea realtime
问题描述
你好,我刚开始学习SignalR v2。我在我的aspx页面中有一个textarea,并且我想要在用户按下按键时实时更新它。
Hi i just started learning SignalR v2. I have a textarea in my aspx page and i want to update it in realtime whenever a user presses a key on it.
问题:我在浏览器中打开了2个标签页。
如果我键入第一个字符示例a,则不会发生任何反应:
Tab1 textarea:a
tab2 textarea:NOTHING
Issue: I opened 2 tabs in my browser. If i type the first character example "a", nothing happens: Tab1 textarea: "a" tab2 textarea: NOTHING
然后我继续输入第二个字符b
Tab1 textarea:ab
tab2 textarea:a
Then i proceed to type the second character "b" Tab1 textarea: "ab" tab2 textarea: "a"
结论:它总是不会更新我输入的最新字符!
Conclude: Its always not updating the latest character i typed!
SignalR Class
SignalR Class
public class DocHub : Hub
{
public void Update(String text)
{
this.Clients.Others.updateText(text);
}
}
index.aspx
<body>
<form id="form1" runat="server">
<div style="font-size:50px;"> New Document</div>
<textarea rows="4" cols="50" id="ta"></textarea>
<script src="Scripts/jquery-1.10.2.js"></script>
<script src="Scripts/jquery.signalR-2.1.2.js"></script>
<script src="signalr/hubs"></script>
<script type="text/javascript">
$(function () {
var hubProxy = $.connection.docHub;
hubProxy.client.updateText = function (msg) {
$("#ta").val(msg);
};
$("#ta").keydown(function () {
hubProxy.server.update($("#ta").val());
});
$.connection.hub.start();
});
</script>
</form>
</body>
推荐答案
我真的会尝试使用键控来代替
I really would try to use keyup instead
这篇关于SignalR实时更新TextArea的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!