JavaScript Shift+Enter (Firefox)

发布于 2024-10-10 04:06:09 字数 543 浏览 1 评论 0原文

看了一下,发现了一些东西,但似乎没有任何效果如我所愿。

最初,我的解决方案适用于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

起风了 2024-10-17 04:06:09

捕获表单中的提交调用 (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 with shift. If yes, return false to the submit request.

You can also try if returning false on e.shiftKey && e=="13" is sufficient.

眼泪都笑了 2024-10-17 04:06:09

您应该能够通过 e.cancel = true; 取消事件传播,但我发现当前您正在覆盖 e 变量(事件对象)。如果您将代码更改为以下内容,应该没问题:

function goReturn(e_obj,str) {

  var e = (window.Event) ? e_obj.which : e_obj.keyCode;
  if (e.shiftKey && e=="13") {
    document.getElementById("wall").value = 
        document.getElementById("wall").value+"\n";
    e_obj.cancel = true;
  } else if(e=="13"){
    // ...continue to send data
  }
}

You should be able to cancel the event propagation through e.cancel = true; but I see that currently you're overwriting the e variable (event object). If you would change your code to the following, you should be fine:

function goReturn(e_obj,str) {

  var e = (window.Event) ? e_obj.which : e_obj.keyCode;
  if (e.shiftKey && e=="13") {
    document.getElementById("wall").value = 
        document.getElementById("wall").value+"\n";
    e_obj.cancel = true;
  } else if(e=="13"){
    // ...continue to send data
  }
}
无法回应 2024-10-17 04:06:09

我为自己的用例编写了一个 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文