jQuery 选择“输入” while 在某些文本元素中并拦截父表单的提交事件

发布于 2024-12-19 02:41:35 字数 502 浏览 0 评论 0原文

当我在某些文本字段中时,我希望能够停止“输入”按键的传播。

基本上,只要我确定输入,我就不希望提交它们所在的表单,并且我需要拦截它。 (表单被分成“窗口”。如果它们不在最后一个窗口(.win2)中,那么我不希望他们能够在没有意识到的情况下按回车键并提交表单只在第一个窗口上,

我希望这是有道理的

$(".win1 :input").keyup(function(e){
    if(e.which === 13){
        return false;
    }
});

不幸的是,这不起作用,但我认为 return false 不起作用。适当地使用。 e.preventDefault(); e.stopPropagation(); 也不起作用。是否有一种简单的方法可以通过监听来阻止表单上的 submit 事件触发。 “输入”键按下

When I'm in certain text fields I want to be able to stop the propagation of the "enter" keypress.

Basically as long as I'm certain inputs I don't want the form that they're in to submit, and I need to intercept it. (The form is split into "windows". If they're not in the last window (.win2) then I don't want them to be able to hit enter and submit the form without realising they're only on the first window.

I hope that makes sense.

Here's what I have:

$(".win1 :input").keyup(function(e){
    if(e.which === 13){
        return false;
    }
});

Unfortunately this doesn't work. The keyup event gets fired, but I don't think that the return false works appropriately. Using e.preventDefault(); e.stopPropagation(); doesn't work either. Is there an easy way to stop the submit event from triggering on the form by listening to "enter" key presses?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

情痴 2024-12-26 02:41:35

您可以将输入 type="submit" 替换为普通按钮,如果数据合法,它将执行类似 $("form").submit(); 的操作,否则永远不会提交触发。

通常这应该有效:

$(".win1 :input").keypress(function(e){
  if(e.which === 13){
        e.preventDefault();
        e.stopPropagation();
    }
});

You can replace your input type="submit" by a normal button, which will do something like $("form").submit(); if the data is legal, and otherwise the submit is never triggered.

Normally this should work:

$(".win1 :input").keypress(function(e){
  if(e.which === 13){
        e.preventDefault();
        e.stopPropagation();
    }
});
一瞬间的火花 2024-12-26 02:41:35

也许你可以使用这个例子:

   <html>
  <head>
  <script type="text/javascript">
  function CancelSubmit(e)
  {
    var keynum
    var ret;
    if(window.event) // IE
    {
        keynum = e.keyCode
    }
    else if(e.which) // Netscape/Firefox/Opera
    {
        keynum = e.which
    }

    if(keynum == 13)
    {
        var content = document.getElementById("container").innerHTML;
        document.getElementById("container").innerHTML =   content + "<p>submit canceled</p>";
        ret = false;
    }
    else
    {
        ret = true;
    }
    return ret;

  }
  </script>
  </head>
  <body>
  <h1>CANCEL SUBMIT</h1>

    <form method="post" action="Test-Submit.html">
    <input type="text" onkeypress="return CancelSubmit(event)" />
    <input type="text" onkeypress="return CancelSubmit(event)" />
    <input type="submit" value="submit"/>
    </form>
    <div id="container"></div>
  </body>
  </html>

Maybe you can use this example:

   <html>
  <head>
  <script type="text/javascript">
  function CancelSubmit(e)
  {
    var keynum
    var ret;
    if(window.event) // IE
    {
        keynum = e.keyCode
    }
    else if(e.which) // Netscape/Firefox/Opera
    {
        keynum = e.which
    }

    if(keynum == 13)
    {
        var content = document.getElementById("container").innerHTML;
        document.getElementById("container").innerHTML =   content + "<p>submit canceled</p>";
        ret = false;
    }
    else
    {
        ret = true;
    }
    return ret;

  }
  </script>
  </head>
  <body>
  <h1>CANCEL SUBMIT</h1>

    <form method="post" action="Test-Submit.html">
    <input type="text" onkeypress="return CancelSubmit(event)" />
    <input type="text" onkeypress="return CancelSubmit(event)" />
    <input type="submit" value="submit"/>
    </form>
    <div id="container"></div>
  </body>
  </html>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文