jquery ctrl+enter as enter in text area

后端 未结 6 1932
南旧
南旧 2020-12-30 07:33

I am trying to reproduce standard instant messenger behavior on TEXT area control: enter works as send button. ctrl+enter as real enter.

 $(\"#txtChatMessag         


        
6条回答
  •  时光取名叫无心
    2020-12-30 08:08

    The following will work in all the major browsers, including IE. It will behave exactly as though the enter key had been pressed when you press 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;
        }
    }
    

提交回复
热议问题