admin管理员组

文章数量:1278881

My setup: jQuery 1.6.2

I have this HTML

<textarea class="ment_box"> Write a ment...</textarea>  

And the following Javascript

<script>
$('ment_box').keydown(function (e){
    if(e.keyCode == 13){
        alert('you pressed enter ^_^');
    }
})
</script>

When I press the enter key in the textarea, nothing triggers

EDIT Oops, cut and paste error, I do have $ in my code and it still doesn't work, must be something else going on.

My bad, it is a user operator error, it does work. Sorry for the confusion.

My setup: jQuery 1.6.2

I have this HTML

<textarea class="ment_box"> Write a ment...</textarea>  

And the following Javascript

<script>
$('.ment_box').keydown(function (e){
    if(e.keyCode == 13){
        alert('you pressed enter ^_^');
    }
})
</script>

When I press the enter key in the textarea, nothing triggers

EDIT Oops, cut and paste error, I do have $ in my code and it still doesn't work, must be something else going on.

My bad, it is a user operator error, it does work. Sorry for the confusion.

Share Improve this question edited Aug 29, 2011 at 23:55 Bob asked Aug 29, 2011 at 23:40 BobBob 8,50418 gold badges75 silver badges110 bronze badges 1
  • Guess is a type when you past code here. But ('.ment_box') shouldn't be $('.ment_box')? – Prusse Commented Aug 29, 2011 at 23:42
Add a ment  | 

3 Answers 3

Reset to default 6
$('.ment_box').keypress(function(event) {
    // Check the keyCode and if the user pressed Enter (code = 13) 
    if (event.keyCode == 13) {
        alert('you pressed enter ^_^');
    }
});

Thats it

Check out this answer:

jQuery Event Keypress: Which key was pressed?

var code = (e.keyCode ? e.keyCode : e.which);
 if(code == 13) { //Enter keycode
   //Do something
 }

For jQuery you need to use the $ to specify.

$('.ment_box').keyd

should do it.

本文标签: javascriptjQuery not detecting enter key pressed in textareaStack Overflow