<head> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> <style> #area { resize: none; overflow: hidden; } </style> </head> <body> <script> $(document).ready(function(){ $('#area').keypress(function(e){ if(e.which == 13){ $('form').submit(); } }); }); </script> <textarea id="area" type="text" name="text0" rows="2"></textarea> <input type="submit" value="Отправить"> </body>
ctrl = false; shift = false; $('#abc').keydown(function(event){ switch (event.which) { case 13: return false; case 16: shift = true; case 17: ctrl = true; } }); $('#abc').keyup(function(event){ switch (event.which) { case 13: if (!ctrl && !shift) { $('#fmsg').submit(); return false; } breakText('\r\n'); break; case 16: shift = false; case 17: ctrl = false; } });
// вставка текстов в строку сообщения function breakText(str) { var caret = getCaretPosition('abc'); var text = $('#abc').val(); $('#abc').val(text.substring(0, caret)+str+(text.substring(caret))); setCaretPosition('abc', ($.browser.opera) ? caret+2 : caret+1); } function breakText_rangyinputs() { var caret = $('#abc').getSelection().start; $('#abc').insertText('\r\n', caret, false).setSelection(caret+1, caret+1); } function getCaretPosition(id) { el = document.getElementById(id); if (el.selectionStart) { return el.selectionStart; } else if (document.selection) { el.focus(); var r = document.selection.createRange(); if (r == null) { return 0; } var re = el.createTextRange(), rc = re.duplicate(); re.moveToBookmark(r.getBookmark()); rc.setEndPoint('EndToStart', re); var add_newlines = 0; for (var i=0; i<rc.text.length; i++) { if (rc.text.substr(i, 2) == '\r\n') { add_newlines += 2; i++; } } return rc.text.length + add_newlines; } return 0; } function setCaretPosition(id, pos) { el = document.getElementById(id); if(el.setSelectionRange) { el.focus(); el.setSelectionRange(pos,pos); } else if (el.createTextRange) { var range = el.createTextRange(); range.collapse(true); range.moveEnd('character', pos); range.moveStart('character', pos); range.select(); } }