检测光标位置
我有 25 个组件,其中包括 [textarea、textfile、radio、combo 等...],并且我编写了一个关键事件,以便当输入“ENTER”时,我调用一个将提交页面的函数。
现在,当我按 Enter 键时,我的页面就会被提交,即使在文本区域中也是如此,而这不应该是这样。那么有什么办法可以让我在文本区域按下时无法提交页面呢?
这只发生在 IE7 和 IE8 中;它在所有其他浏览器中都能正常工作。
I have 25 components which includes [textarea, textfile, radio, combo, etc...] and I have written a key event so that when "ENTER" is entered, I call a function which will submit the page.
Now my page is getting submitted when I press enter, even in the textarea which should not be. So is there any way that I can not submit the page if it is pressed in the text area?
This happens only in IE7 and IE8; it works properly in all the other browser.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可能会检测到是否有任何文本区域等未填写/清空/未设置。如果所有内容均正确填写,请发送表格。
you could probably detect if any of the textarea, etc is not filled out/emtpy/unset. if all of them are filled out properly, send the form.
您是否将“关键事件”附加到整个表格中?整个 DOM?如果你这样做了,那是正常行为。
如果您希望在焦点位于提交按钮上时使用“Enter 键”提交页面,则在 onsubmit 事件中应用此功能 - 当然您可以在那里执行所需的所有验证。
如果您只想从文本区域中排除回车键事件 - 执行简单的检查焦点是否位于该时刻的文本区域中。
Did you attach the "key event" to the whole form? The whole DOM? if you did that's a normal behavior.
If you want the "Enter key" to submit the page when the focus is on the submit button then apply this functionality in the onsubmit event - there of course you can perform all the validation you need.
If you just want to exclude the enter key event from the text area - perform a simple check if the the focus is in the textarea that momemnt.
表单的默认行为是,如果用户在表单内按回车键,则提交,除非焦点位于文本区域,因此您想要的是默认行为。删除当前处理表单按键的所有代码,您将获得所需的内容。
The default behaviour of a form is to submit if the user hits enter inside the form unless the focus is on a textarea, so what you want is the default behaviour. Remove whatever code you have that currently handles keypresses for the form and you'll have what you want.
我不确定这是否适合您的需求,但您可以使用如下所示禁用文本区域内的回车键:
I'm not sure if this will suit your needs, but you can disable the enter key inside the textarea with something like this: