简体   繁体   English

jquery ctrl +在文本区域输入as

[英]jquery ctrl+enter as enter in text area

I am trying to reproduce standard instant messenger behavior on TEXT area control: enter works as send button. 我试图在TEXT区域控件上重现标准的即时消息行为:输入作为发送按钮。 ctrl+enter as real enter. ctrl +输入为真实输入。

 $("#txtChatMessage").keydown(MessageTextOnKeyEnter);
 function MessageTextOnKeyEnter(e)
 {
    if (!e.ctrlKey && e.keyCode == 13)
    {
       SendMessage();
       return false;
    }
    else if(e.keyCode == 13)
    {
       $(this).val($(this).val() + "\n");
    }
    return true;
 }

I have tried with both commented line and without. 我已尝试使用注释行和不使用。 Not works. 不行。 simple enter works as expected. 简单的输入按预期工作。 Any ideas how to add enter on ctrl+enter? 任何想法如何在ctrl + enter上添加回车?

key code is not problem. 关键代码没问题。 they are detected correctly. 它们被正确检测到。 so all if's works as expected. 所以如果按预期工作的话。 But appending new line works incorrectly (in FF, Chrome works correctly). 但添加新行的工作不正确(在FF中,Chrome可正常工作)。 So I need correct multibrowser way to insert new line symbol to textarea. 所以我需要正确的多浏览器方式将新行符号插入textarea。 If without adding string manually (by some event based on ctrl+enter) it will be better. 如果没有手动添加字符串(通过基于ctrl + enter的某些事件),它会更好。

changing on keypress event has no effect. 更改按键事件无效。 "\\r\\n" not helped. “\\ r \\ n”没有帮助。

test page located here 测试页面位于此处

The following will work in all the major browsers, including IE. 以下内容适用于所有主流浏览器,包括IE。 It will behave exactly as though the enter key had been pressed when you press ctrl-enter: 当你按下ctrl-enter时,它的行为就像按下回车键一样:

function MessageTextOnKeyEnter(e) {
    if (e.keyCode == 13) {
        if (e.ctrlKey) {
            var val = this.value;
            if (typeof this.selectionStart == "number" && typeof this.selectionEnd == "number") {
                var start = this.selectionStart;
                this.value = val.slice(0, start) + "\n" + val.slice(this.selectionEnd);
                this.selectionStart = this.selectionEnd = start + 1;
            } else if (document.selection && document.selection.createRange) {
                this.focus();
                var range = document.selection.createRange();
                range.text = "\r\n";
                range.collapse(false);
                range.select();
            }
        }
        return false;
    }
}

A few potential causes: 一些潜在的原因:

Define the function before you reference it. 在引用之前定义函数。

Make sure you're binding the event in the document.ready event, so that the dom item exists when you reference it. 确保您在document.ready事件中绑定事件,以便在引用它时存在dom项。

Change else (e.keyCode == 13) to else if (e.keyCode == 13) . 改变else (e.keyCode == 13)else if (e.keyCode == 13)

Make sure this is a textarea , not an input[type=text] . 确保这是一个textarea ,而不是input[type=text]

Consider using keypress instead of keydown . 考虑使用keypress而不是keydown

Some browsers will send keyCode == 10 instead of keyCode == 13 when using the ctrl modifier key (some browsers will send it even when you aren't using the ctrl modifier key). 当使用ctrl修饰键时,某些浏览器将发送keyCode == 10而不是keyCode == 13 (即使您不使用ctrl修饰键,某些浏览器也会发送它)。

wow what a pain in the ass. 哇屁股什么痛苦。 i've been playing with this for a while and i have to assume this is just IE being uncooperative. 我已经玩了一段时间了,我不得不假设这只是IE不合作。 anyway, this is the best i could do this morning and it's super hacky, but maybe you can gain something from it. 无论如何,这是我今天早上能做的最好的,而且它超级黑客,但也许你可以从中获得一些东西。

explanation: i'm testing with ie8, a textarea element, and courier new. 解释:我正在测试ie8,textarea元素和courier new。 results may vary. 结果可能有所不同 ascii character 173 (0xAD) does not display a character, although it counts as a character when moving your cursor around. ascii character 173(0xAD)不显示字符,虽然它在移动光标时计为字符。 appending this char after you force a newline gets ie to move the cursor down. 强制换行后附加此字符即可将光标向下移动。 before the call to SendMessage we replace the extra char with nothing. 在调用SendMessage之前,我们用什么都不替换额外的char。

function MessageTextOnKeyEnter(e) 
 { 
    var dummy = "\xAD";
    if (!e.ctrlKey && e.keyCode == 13) 
    { 
        var regex = new RegExp(dummy,"g");
        var newval = $(this).val().replace(regex, '');
        $(this).val(newval);

       SendMessage(); 
       return false; 
    } 
    else if(e.keyCode == 13) 
    { 
       $(this).val($(this).val() + "\n" + dummy); 
    } 
    return true; 
 } 

if you try to do the replacement on every keystroke it's not going to work very well. 如果你试图在每次击键时进行替换,那么它将无法正常工作。 you might be able to deal with this by white/blacklisting keys and find a method to put the cursor back in the text where it's supposed to go. 您可以通过白名单/黑名单键来处理这个问题,并找到一种方法将光标放回到它应该去的文本中。

My answer leads from Ian Henry's answer about keyCode == 10, which seems to be the case in IE (tested in 8 & 9). 我的答案来自Ian Henry关于keyCode == 10的回答,这似乎是IE中的情况(在8和9中测试)。 Check if you are dealing with a windows event and ket the key code. 检查您是否正在处理Windows事件并键入密钥代码。

  $('#formID #textareaID').keypress(function(e) {
    if(window.event) {
      var keyCode = window.event.keyCode;     
    }
    else {
      var keyCode = e.keyCode || e.which;
    }

    if( (!e.ctrlKey && (keyCode == 13)) ) {
      //do stuff and submit form
    }
    else if( (e.ctrlKey && (keyCode == 13)) || (keyCode == 10) ) {
      //do stuff and add new line to content
    }                  
  });

You can check for the ctrl and alt as in 你可以检查ctrl和alt

$(document).ready(function() {
    $('#myinput').keydown(function(e) {
        var keysare = 'key code is: ' + e.which + ' ' + (e.ctrlKey ? 'Ctrl' : '') + ' ' + (e.shiftKey ? 'Shift' : '') + ' ' + (e.altKey ? 'Alt' : '');
        //alert(keysare);
        $('#mycurrentkey').text(keysare);
        return false;
    });
});

See a working example here: http://jsfiddle.net/VcyAH/ 在这里查看一个工作示例: http//jsfiddle.net/VcyAH/

If you can stick with Shift+Enter instead of Ctrl+Enter then the solution is trivial. 如果您坚持使用Shift + Enter而不是Ctrl + Enter,则解决方案很简单。 You don't need any special code as Shift+Enter triggers a line break automatically. 您不需要任何特殊代码,因为Shift + Enter会自动触发换行符。 Just catch plain Enter to do the sending. 只需抓住普通Enter即可进行发送。

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

相关问题 Ctrl +在TEXTAREA中输入jQuery - Ctrl+Enter jQuery in TEXTAREA 如何使用jQuery将Ctrl + Enter绑定到Ajax表单提交 - How to use jquery to bind ctrl+enter to ajax form submission tinyMCE:如何添加快捷键Ctrl + Enter - tinyMCE: how to add shortcut Ctrl+Enter CKEditor:使Shift + Enter与Ctrl + Enter相同 - CKEditor: Make Shift+Enter same as Ctrl+Enter textarea 控件 - 自定义行为 enter/ctrl+enter - textarea control - custom behavior enter/ctrl+enter 当用户在 Ext.form.field.HtmlEditor 中输入文本时,按 Ctrl+Enter - Catch Ctrl+Enter when user typing text in Ext.form.field.HtmlEditor 在Opera中按Ctrl + Enter(单击)避免使用新选项卡 - Avoid new tabs on Ctrl+Enter(Click) in Opera browserstack ctrl+enter 键盘事件未在 chrome 或 IE 中注册 - browserstack ctrl+enter keyboard event not registering in chrome or IE 用于在GitHub中新发行页面的标题文本框中按Ctrl + Enter或Enter时创建确认弹出窗口的用户脚本 - Userscript for creating a confirmation popup whenever pressing Ctrl+Enter or Enter in the title textbox of a new issue page in GitHub 按住'ctrl + enter'可以为按键事件提供不同的键码,而不仅仅是直接'输入' - 但仅限于Windows - Holding down 'ctrl+enter' gives a different keycode for keypress event than just straight 'enter' - But only on Windows
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM