开发者

Execute function by pressing "return" using jQuery?

开发者 https://www.devze.com 2022-12-23 04:24 出处:网络
i have two text inputs like the following, i don\'t want to use <form> , so i want when people press \"return\" BUTTON after filling the inputs, a function called \"doit()\" should be executed.

i have two text inputs like the following, i don't want to use <form> , so i want when people press "return" BUTTON after filling the inputs, a function called "doit()" should be executed.

<script>
function doit(){
alert("you submitted the info");
..........ajax code........
}
</script>

<input type="text" id="email" />
<input type="text" id开发者_如何学Python="skills" />

Thanks


The following will do what you are looking for.

$('#emails, #skills').keypress(function(e){
   if( e.keyCode == $.keyCode.ENTER || e.keyCode == $.keyCode.NUMPAD_ENTER ){
     yourSubroutine();
   }
});


$('#name, #last').bind('keydown', function(e) {
                    if (e.keyCode == 13 || e.keyCode == 108) {
                        doit();
                    }
                });

KeyCode can be found here


Check out this question. Essentially, you'll want to bind a keypress listener, and then check for the return key.

0

精彩评论

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