Skip to content

Instantly share code, notes, and snippets.

@chzyer
Last active December 10, 2015 22:48
Show Gist options
  • Save chzyer/4504816 to your computer and use it in GitHub Desktop.
Save chzyer/4504816 to your computer and use it in GitHub Desktop.
谈ctrl+enter提交那些事

最近逛了很多博客,发现都有Ctrl+Enter提交评论功能...你也有吧? 小弟OUT啦,没有! 想当初,第一次遇到这功能的是在QQ2000上看见的吧,一直用得挺习惯的。秉承着自己动手,丰衣足食的意念,我就亲手添加这个小功能吧。 我的思路是,当ctrl键keydown时,变量a=1,当ctrl键keyup时,变量a=0;当enter键keypress时,且a==1时,submit! 然后开工,测得ctrl的keycode为17,enter的keycode为13,然后就.........未完!

好吧,其实真正上没有这么麻烦..我承认我又OUT了,在我同时按下ctrl+enter键时,竟然发现keycode=10 !! 按ASCII码算是\r ?! 不理了,绕了一圈,直接

$("textarea").keypress(function(e){
	if (e.keyCode==10) submit();//提交
})

我OUT啦,我OUT啦 感谢phoetry兄的指点。上面的效果仅仅是chrome和ie有效,而当时我也仅仅刚刚好测试了chrome和ie...ff和opera不兼容。 新代码新代码...

$("textarea").keypress(function(e){
	if(e.ctrlKey && e.which == 13 || e.which == 10) submit(); //提交
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment