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

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

原文链接:http://liuna718-163-com.iteye.com/blog/808850

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值