简体   繁体   中英

How to call keypress event of button by pressing ENTER in text box

In my application I have one button and one text box. Now I have call one function of jQuery on click and keypress event of button.

now i want that the same function is call when the ENTER key press on text box.

is there any way to call that jQuery function?

I have not form this both elements are work without form

Try this

$('#search_btn').keypress(function(e) {
    if(e.which == 13) {

        alert("Enter pressed");
       //Call your function here, or trigger the button click.
    }
});

DEMO: http://jsfiddle.net/skhan/brdaj/

you can use keypress() with jquery , like this :

$(document).keypress(function(e) {
    if(e.which == 13) {
        console.log("2");
    }
});

Check FIDDLE

If you dont want to make with jquery,( form onsubmit or javascript ) check this source :

Detect enter key pressed using javascript

and this one;

Enter key press event in JavaScript

This should do what you want:

$('body').keyup(function(e){
   if(e.which == 13 && $('#yourText').is(':focus')){
     console.log("do your thing");
   }
});

http://jsfiddle.net/L2M3Y/1/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM