路人x山崎宗介:键盘回车事件导致页面刷新的问题
来源:百度文库 编辑:九乡新闻网 时间:2024/05/08 10:04:50
键盘回车事件导致页面刷新的问题
2010-03-09
我最近在做一个 Ajax 查询的功能,代码如下:
1
<
form
name
=
"keywordForm"
method
=
"post"
action
=
""
>
2
<
p
id
=
"profile_nav"
>
3
<
label
for
=
"profile"
> 关键字搜索:
label
>
4
<
input
style
=
"width:80; height:20"
type
=
"text"
name
=
"keyword"
onkeypress
=
"searchKeywordKeyboard(event)"
/>
5
<
input
type
=
"button"
value
=
"搜索"
onClick
=
"searchKeyword()"
>
6
p
>
7
form
>
在文本框中输入关键字按回车,页面自动刷新了,结果肯定是没有实现无刷新搜索了。想了想,可能是按回车后默认提交了表单,于是将form去掉,果然不刷了。但是还是会有很多地方需要用到form。
一个表单下,如果只有一个文本框时,按下回车将会触发表单的提交事件。
既然是只有一个文本框才会出问题,那么可以加一个隐藏的文本框,如下:
1
<
input
id
=
"hiddenText"
type
=
"text"
style
=
"display:none"
/>
现在代码成了这样:
1
<
form
name
=
"keywordForm"
method
=
"post"
action
=
""
>
2
<
p
id
=
"profile_nav"
>
3
<
label
for
=
"profile"
> 关键字搜索:
label
>
4
<
input
style
=
"width:80; height:20"
type
=
"text"
name
=
"keyword"
onkeypress
=
"searchKeywordKeyboard(event)"
/>
5
<
input
id
=
"hiddenText"
type
=
"text"
style
=
"display:none"
onkeypress
=
"searchKeywordKeyboard(event)"
/>
6
<
input
type
=
"button"
value
=
"搜索"
onClick
=
"searchKeyword()"
>
7
p
>
8
form
>
结论是,可以采取两种方法解决这种问题:1.去掉表单;2.如果非得用表单,只要不让表单里有且只有一个文本框就OK了。
如果以上的方法还不足以让你去解决问题,那么你可以用以下方法来阻止因为回车而引起的表单自动提交:
1
<
form
name
=
"keywordForm"
method
=
"post"
action
=
""
onsubmit
=
"return false;"
>
2
<
p
id
=
"profile_nav"
>
3
<
label
for
=
"profile"
> 关键字搜索:
label
>
4
<
input
style
=
"width:80; height:20"
type
=
"text"
name
=
"keyword"
onkeypress
=
"searchKeywordKeyboard(event)"
/>
5
<
input
id
=
"hiddenText"
type
=
"text"
style
=
"display:none"
onkeypress
=
"searchKeywordKeyboard(event)"
/>
6
<
input
type
=
"button"
value
=
"搜索"
onClick
=
"searchKeyword()"
>
7
p
>
8
form
>
就是在表单 form 后面加上一个 onsubmit 事件,返回 false,来阻止 form 提交。
键盘回车事件导致页面刷新的问题
ASP.NET教程之实现页面无刷新的方法
asp.net 的页面执行顺序 页面事件执行详解
人生的键盘:复制、另存为、移动、刷新、回收站、删除
人生的键盘:复制、另存为、移动、刷新、回收站、删除
js刷新页面方法大全
神奇好美的音乐空间【每刷新一次,换一次页面和歌曲专辑】
神奇好美的音乐空间【每刷新一次,换一次页面和歌曲专辑】 。
神奇的音乐空间__每刷新一次,换一次页面和歌曲专辑
神奇好美的音乐空间【每刷新一次,换一次页面和歌曲专辑】
神奇好美的音乐空间【每刷新一次,换一次页面和歌曲专辑】
js页面刷新(本页,父页)
关于linux和windows的CR, LF, CR/LF 回车 换行问题
解决VFP中表格控件Grid的数据源刷新问题
婚姻中猜忌导致的自焚事件
javascript键盘事件总结
封装键盘事件2.0
window.onload 关于页面加载完毕的问题
刷新一次页面,图片就改变一次 ,可有意思了。
《好听歌曲大集合,刷新页面更换》【精美音画】
导致夫妻关系紧张的不良沟通问题
导致夫妻关系紧张的不良沟通问题
如何去掉多余的回车符
批量删除Word中回车的方法