## Slideshow (357x214) ##
# Example: $photo1 = "http://web2themes.com/demo/wp-content/uploads/2007/06/picture1.gif"; $url1 = "http://web2themes.com/demo/?p=17"; #
$photo1 = "http://jianyun.org/wordpress/wp-content/themes/sodelicious2_green/images/1.jpg";
$url1 = "http://jianyun.org";
$photo2 = "http://jianyun.org/wordpress/wp-content/themes/sodelicious2_green/images/2.jpg";
$url2 = "http://jianyun.org";
$photo3 = "http://jianyun.org/wordpress/wp-content/themes/sodelicious2_green/images/3.jpg";
$url3 = "http://jianyun.org";
$photo4 = "http://jianyun.org/wordpress/wp-content/themes/sodelicious2_green/images/4.jpg";
$url4 = "http://jianyun.org";
$photo5 = "http://jianyun.org/wordpress/wp-content/themes/sodelicious2_green/images/5.jpg";
$url5 = "http://jianyun.org";
?>
如果在页面上有多个Button,有的时候我们希望通过按Enter回车键来实现点击某个Button的效果。
怎么做呢?
用jQuery可以给页面上的元素加上一个keypress事件,来侦测用户按的是不是Enter键,从而做出处理,比如:
1
2
3
4
5
6
7
8
| $(document).ready(function() {
$("body").keypress(function(event) {
switch (event.keyCode) {
case 13:
$("#<%=ButtonNext.ClientID %>").click();
}
});
}); |
但是,如果页面上有多行文本框TextArea,按Enter键其实是要换行而不是提交页面。或者有需求就是当在输入框中时,按Enter键就是不想提交页面,怎么办呢?我们可以在刚刚的基础在,再在输入控件上加事件来判断,如果按的是回车键的话,就忽略不做额外处理。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| $(document).ready(function() {
$("body").keypress(function(event) {
switch (event.keyCode) {
case 13:
$("#<%=ButtonNext.ClientID %>").click();
}
});
//在所有input元素上绑一个keypress事件
$(":input").keypress(function(event) {
switch (event.keyCode) {
//通过调用stopPropagation()来阻止事件冒泡
case 13: event.stopPropagation();
}
});
}); |
这样,就实现了在input元素上按回车不做处理,而其他时候按回车键实现点击页面上某个按钮的效果。
相关文章
print"$ads2"; ?>
Author: jianyun
Tags: jQuery,
回车提交
订阅评论
TrackBack
## Slideshow (357x214) ##
# Example: $photo1 = "http://web2themes.com/demo/wp-content/uploads/2007/06/picture1.gif"; $url1 = "http://web2themes.com/demo/?p=17"; #
$photo1 = "http://jianyun.org/wordpress/wp-content/themes/sodelicious2_green/images/1.jpg";
$url1 = "http://jianyun.org";
$photo2 = "http://jianyun.org/wordpress/wp-content/themes/sodelicious2_green/images/2.jpg";
$url2 = "http://jianyun.org";
$photo3 = "http://jianyun.org/wordpress/wp-content/themes/sodelicious2_green/images/3.jpg";
$url3 = "http://jianyun.org";
$photo4 = "http://jianyun.org/wordpress/wp-content/themes/sodelicious2_green/images/4.jpg";
$url4 = "http://jianyun.org";
$photo5 = "http://jianyun.org/wordpress/wp-content/themes/sodelicious2_green/images/5.jpg";
$url5 = "http://jianyun.org";
?>
1 Comment(s)