JavaScript Shift+Enter (Firefox)
看了一下,发现了一些东西,但似乎没有任何效果如我所愿。
最初,我的解决方案适用于 Internet Explorer 和 Chrome,但不适用于 Firefox(这对我来说无法正常工作是令人不满意的)
我正在寻找的是一个简单的文本区域,它在 Enter 键上发送数据,但创建一个新行按 Shift+Enter。以下是我所拥有的
function goReturn(e,str) {
var e = (window.Event) ? e.which : e.keyCode;
if (e.shiftKey && e=="13") {
document.getElementById("wall").value =
document.getElementById("wall").value+"\n";
} else if(e=="13"){
// ...continue to send data
}
}
这在输入时发送数据,但也在移位和输入时发送数据(这是我遇到的问题)。
感谢您的帮助
Had a look and found some things on this, but nothing seems to work as I'd like it.
Initially I had my solution working with internet explorer and chrome, but not firefox (which is unsatisfactory for me to not have working)
What I'm looking for is a simple text area, which sends data on enter key, but creates a new line on Shift+Enter. The following is what I have
function goReturn(e,str) {
var e = (window.Event) ? e.which : e.keyCode;
if (e.shiftKey && e=="13") {
document.getElementById("wall").value =
document.getElementById("wall").value+"\n";
} else if(e=="13"){
// ...continue to send data
}
}
This sends the data on enter, but also sends the data on shift and enter (which is the problem I have).
Thanks for any assistance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
捕获表单中的提交调用 (onsubmit="...")。在那里,检查最后一个
enter
是否与shift
组合。如果是,则返回 false 提交请求。您还可以尝试在
e.shiftKey && 上返回
就足够了。false
e=="13"Catch the submit call in your form (onsubmit="..."). There, check if the last
enter
was in combination withshift
. If yes, return false to the submit request.You can also try if returning
false
one.shiftKey && e=="13"
is sufficient.您应该能够通过
e.cancel = true;
取消事件传播,但我发现当前您正在覆盖e
变量(事件对象)。如果您将代码更改为以下内容,应该没问题:You should be able to cancel the event propagation through
e.cancel = true;
but I see that currently you're overwriting thee
variable (event object). If you would change your code to the following, you should be fine:我为自己的用例编写了一个 jQuery 插件,它可能适合您的: http://cburgmer.github .com/jquery-shiftenter/
该插件负责处理返回键,并在焦点上显示提示以解释行为。
I have written a jQuery plugin for my own use case and it might fit yours: http://cburgmer.github.com/jquery-shiftenter/
The plugin takes care of handling the return key and also displays a hint on focus to explain the behaviour.