[英]Auto insert message in textarea/text when user(s) hit the enter
I'm making simple chatroom, I have textarea in index.php where user can input his/her message. 我正在建立简单的聊天室,在index.php中有textarea,用户可以在其中输入他/她的消息。 I used link to pass the values in ajax instead of button.
我使用链接在ajax中而不是button中传递值。 In my situation, I usually, click the "send" link in order to pass the values.
在我的情况下,我通常单击“发送”链接以传递值。 But how can I solve this when user hit enter his/her message and automatically, display to the div?
但是,当用户点击输入信息并自动显示给div时,我该如何解决呢?
** **
** **
<form name="form1" onsubmit="submitAjaxQuery(event)" >
<textarea name="msg"></textarea><br/>
<a href="#" onclick="submitChat()"> Send </a>
</form>
function submitChat(){
if( form1.msg.value ==''){
alert("All fiels are mandatory");
return;
}
form1.uname.readonly=true;
form1.uname.style.border='none';
var uname= form1.uname.value;
var msg = form1.msg.value;
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4 && xmlhttp.status ==200)
{
document.getElementById('chatlogs').innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open('GET','insert.php?uname='+uname+'&msg='+msg,true);
xmlhttp.send();
}
Hi You can do like this. 嗨,你可以这样做。
$("textarea").keydown(function(e)
{
if (e.keyCode == 13)
{
submit();
}
}
Generally, the thing to do is to add an "onKeyDown" handler that looks to see which key triggered the event, and do a submit if the key is the magic number 13
which is the carriage return. 通常,要做的事情是添加一个“ onKeyDown”处理程序,以查看哪个键触发了该事件,如果键是幻数
13
(即回车),则进行提交。 Looks something like this: 看起来像这样:
var onKeyDown = function(e) {
if (e.which == 13) {
e.preventDefault();
submitChat();
}
};
$("#id-to-text-area").on("keydown", onKeyDown);
**Add Handle() function onkeypress event**
<form name="form1" onsubmit="submitAjaxQuery(event)" >
<textarea name="msg"></textarea><br/>
<a href="#" onclick="submitChat()" onkeypress="handle()"> Send </a>
</form>
function handle(e){ {
if(e.which == 13) {
if( form1.msg.value ==''){
alert("All fiels are mandatory");
return;
}
form1.uname.readonly=true;
form1.uname.style.border='none';
var uname= form1.uname.value;
var msg = form1.msg.value;
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4 && xmlhttp.status ==200)
{
document.getElementById('chatlogs').innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open('GET','insert.php?uname='+uname+'&msg='+msg,true);
xmlhttp.send();
}
}
Please try like this, 请这样尝试
$("textarea").keypress(function(event){
var p = event.which;
if(p==13){ // Enter key press
// Your code goes here
}
});
you can use this code for enter event . 您可以将此代码用于enter事件。 here js fiddle
这里js小提琴
html code HTML代码
<form name="form1" onsubmit="submitAjaxQuery(event)" >
<textarea id="area" name="msg"></textarea><br/>
<a href="#" onclick="submitChat()"> Send </a>
</form>
javascript javascript
$("#area").keypress(function(e) {
if(e.which == 13) {
submitChat();
alert('message submit!');
}
});
Update 更新资料
you may need to add jquery library .. 您可能需要添加jquery库..
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
$("textarea").keypress(function(event) {
if (event.which == 13) {
event.preventDefault();
$("form").submit();
}
});
You can try to detect when the return key is pressed inside your [name=msg] textarea. 您可以尝试检测何时在[name = msg]文本区域内按下返回键。 This is vanilla JS (but you can also use jQuery for this) to add at the bottom of your page.
这是普通的JS(但您也可以使用jQuery )在页面底部添加。
<script type="text/javascript">
function listenKeys(evt) {
var evt = (evt) ? evt : ((event) ? event : null);
var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
if ((evt.keyCode == 13) && (node.name=="msg")) {
// RETURN KEY DETECTED IN msg, EXECUTE YOUR CODE HERE
}
}
document.onkeypress = listenKeys;
</script>
Hope it helps 希望能帮助到你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.