form 填完文本框安enter键表单自动提交

本文探讨了在只有一个单行文本输入框的Web表单中,按下回车键时不会触发按钮的Click事件的问题,并提供了三种解决方案:1. 为form元素添加onsubmit事件并返回false阻止默认行为;2. 在form中隐藏一个额外的输入框;3. 使用button元素代替submit,并通过点击事件手动调用表单提交。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在表单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()'>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值