开发者

Textarea newline prevention on Enter (jQuery)

开发者 https://www.devze.com 2023-01-07 09:15 出处:网络
I have: $(\'#myTextArea\').keyup(function(e) { if(e.keyCode == 13) { e.preventDefault(); // Makes no difference

I have:

$('#myTextArea').keyup(function(e) {
     if(e.keyCode == 13) {
       e.preventDefault(); // Makes no difference
     $(this).parent().submit(); // Submit form it belongs to
   }
});

How do I prevent the newline that shows up right before form submission? prevent开发者_C百科Default() and stopPropagation don't work. I could manually reset the field text, but...


This happens because keyup is called after the text has been inserted in the text area.

To catch the key before being entered you need to listen for the keydown event.

So just change the keyup to keydown and you should be good to go.

$('#myTextArea').keydown(function(e) {
0

精彩评论

暂无评论...
验证码 换一张
取 消