jquery ctrl+enter as enter in text area

后端 未结 6 1944
南旧
南旧 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 07:49

    My answer leads from Ian Henry's answer about keyCode == 10, which seems to be the case in IE (tested in 8 & 9). Check if you are dealing with a windows event and ket the key code.

      $('#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
        }                  
      });
    

提交回复
热议问题