在表单form中,包含text和submit,当焦点在text内,如果直接按回车,页面不会像点击submit按钮那样执行验证代码,会直接提交掉。(基本发生在表单内只有一个text文本域的时候)
If you have a Web Form with just one single-line text input (even if there are additional input fields, like CheckBoxLists, RadioButtonLists, DropDownLists, and so on), hitting enter in the text input will not cause the Button Web control's Click event to fire when using Internet Explorer
解决方法:
1.增加 form 的 onsubmit 事件来阻止表单自动提交:(IE和火狐下测试通过,推荐)
<form name="testForm" method="post" action="/testAction.do" onsubmit="return false;">
2.在页面 form 中增加一个 <input style="display:none">
3.用 button 取代 submit :
<input type='button' value='确定' onclick='document.formname.submit()'>
本文探讨了在只有一个单行文本输入框的Web表单中,按下回车键时不会触发按钮的Click事件的问题,并提供了三种解决方案:1. 为form元素添加onsubmit事件并返回false阻止默认行为;2. 在form中隐藏一个额外的输入框;3. 使用button元素代替submit,并通过点击事件手动调用表单提交。
149

被折叠的 条评论
为什么被折叠?



