防止输入按钮提交表单的干净方法
我见过很多 Javascript 解决方案来做到这一点,但我确信一定有一种更简单的方法。
我有一个非常简单的表单 - 一个多行文本框和一个提交按钮。我希望用户能够提交“格式化”文本(即像电子邮件一样,带有段落、换行符等)
但是,当用户按 Enter 键输入回车符时,它会提交表单。
我在那里必须有一个属性或其他东西来控制它,因为这一定是一个常见问题。 Javascript 作为解决方案似乎有点太复杂了。
I've seen a lot of Javascript solutions to do this, but I'm sure there must be an easier way.
I have a really simple form - one multiline textbox and a submit button. I want the user to be able to submit "formatted" text (i.e. like an email, with paragraphs, new lines etc)
However, when the user hits Enter to put in a carriage return it submits the form.
I'm there there must be a property or something that controls this, as this must be a common issue. Javascript as a solution seems a bit too complex.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
将提交按钮上的UseSubmitBehavior属性设置为FALSE。 (此处找到了解决方案)
Set the UseSubmitBehavior property on the submit button to FALSE. (found the solution here)
以下代码解决了我的问题:
http:// www.itorian.com/2012/07/stop-from-submission-posting-on-enter.html
将其放在母版页中或仅放在您想要的页面中。
The following code solved my issue:
http://www.itorian.com/2012/07/stop-from-submission-posting-on-enter.html
Put this in the master page or just in the pages that you want.
使用提交行为对我不起作用,但这
对我有用
Use Submit behavior didn't work for me but this
worked for me
您可以设置 DefaultButton将文本框周围的表单或面板上的不可见(显示:无)按钮。通过这种方式,您可以完全控制当用户按下回车键时会发生什么,而无需(依赖于浏览器)Javascript。如果你不处理它的onclick事件,回车键将被抑制。
http://geekswithblogs.net/ranganh/archive/2006/04/12 /74951.aspx
You could set the DefaultButton on the Form or the Panel surrounding your TextBox to an invisible(display:none) Button. On this way you have full control what happens when user hits enter-key without (browser-dependent) Javascript. If you don't handle its onclick-event, the enter-key will be suppressed.
http://geekswithblogs.net/ranganh/archive/2006/04/12/74951.aspx
与Max的答案类似,但显示完整的代码:
当然,也与其他答案类似,否则它不会工作,但可能更简单。
Similar to the answer of Max, but showing complete code:
Also similar to other answers, of course, otherwise it wouldn't work, but possibly simpler.
你尝试过这个吗? - 在文本区域中输入密钥
Did u try this? - Enter key in textarea
在 HTML 前端只需将 body 标签替换为 body onkeydown = "return (event.keyCode!=13)
In HTML front end just replace body tag with body onkeydown = "return (event.keyCode!=13)