读书人

表单中只有一个文本框时回车页面刷新

发布时间: 2012-10-25 10:58:57 作者: rapoo

表单中只有一个文本框时,回车页面刷新错误

在一个也没中如果一个form标签中只有文本框<input type="text" />,当在输入完数据后点击回车,会发现页面进行了刷新,代码如下:

<body><form><input type="hidden" id="contextPath" name="contextPath" value="<%=request.getContextPath()%>" /><textarea rows="2" cols="2" name="test"></textarea><input type="text" name="noticeNo" id="noticeNo"/></form></body>

有如下解决方法:

1.在文本域元素中加入onkeydown或者onkeypress事件,判断如果用户点击了回车就阻止默认的行为。

<body><form><input type="textsdfsd" name="noticeNo"  onkeypress="if(event.keyCode==13||event.which==13){return false;}" /></form></body>

?

2.在form中在加入一个隐藏的文本域

<input type="text" name="test" style="display:none"/>

<body><form><input type="hidden" id="contextPath" name="contextPath" value="<%=request.getContextPath()%>" /><textarea rows="2" cols="2" name="test"></textarea><input type="text" name="noticeNo" id="noticeNo"/><input type="text" name="test" style="display:none"/></form></body>

?说明:大家可以发现,里面是没有提交按钮的即

<input type="sumit" />,要是里面有提交按钮的话,第二种方法时不使用的,只能使用第一种方法,因为通过查看你会发现,当你点击一个表单时,或者表单的任何元素会发现,提交按钮时激活状态,所以单点击回车时就执行了提交操作。

读书人网 >Web前端

热点推荐