如何捕获包含多个表单的页面上按下的回车键?

发布于 2024-07-16 18:23:55 字数 552 浏览 12 评论 0原文

我继承了一个网络应用程序,其中在任何输入字段中按回车键的通常功能已被禁用,原因很充分,因为页面包含多个表单,并且应用程序将无法确定(或者,所以我被告知) 采取哪种形式采取行动。 该应用程序的设计没有提交按钮(如输入类型=“提交”),相反,设计者采用了 onclick 处理。 以下是在其中一页上定义的两个按钮,用于说明

<input type="button" value="LOGIN"  name="btnLoginOk" onclick="submit(); />"

<input type="button" class="button-click-grey" value="Find Link Partners"  
    onclick="raiseEvent('SubmitForm','',this);" style="cursor:pointer;" />

但我真的希望能够允许用户按回车键(如果他们愿意),例如,如果他们刚刚在与登录相关的字段中输入内容,然后检测到并执行 onclick="submit();"

或许 jQuery 有一个解决方案。

I have inherited a web application where the usual ability to press return in any of the input fields has been disabled for the very good reason that the page contains multiple forms, and the application would not be able to determine (or, so I am told) which form to action. The application has been designed so there are no submit buttons (as in input type="submit") and, instead, the designers have gone for onclick handling. Here are two buttons that are defined on one of the pages, included for illustration

<input type="button" value="LOGIN"  name="btnLoginOk" onclick="submit(); />"

<input type="button" class="button-click-grey" value="Find Link Partners"  
    onclick="raiseEvent('SubmitForm','',this);" style="cursor:pointer;" />

But I really want to be able to allow users to press return if they wish e.g. if they've just typed into a field associated with the LOGIN, then detect that and action the onclick="submit();"

Perhaps there is a solution with jQuery.

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

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

发布评论

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

评论(4

凤舞天涯 2024-07-23 18:23:55

页面包含多个表单,并且
应用程序将无法
确定(或者,据我所知)哪个
从形式到行动。

当您在输入控件上按 Enter 时,浏览器会查找该表单中的第一个提交按钮并模拟单击它。 在有多个按钮的情况下,将在键盘输入时按下第一个按钮(这绝不是一成不变的,浏览器可能会偏离这一点)。

如果您有两个表单,则按下按键的表单将按下第一个提交按钮。 因此,您实际上不需要对此进行任何特殊处理。 你只需要停止妨碍。

您可以在窗体上的代码中模拟此操作:

 $( 'form' ).bind('keypress', function(e){
   if ( e.keyCode == 13 ) {
     $( this ).find( 'input[type=submit]:first' ).click();
   }
 });

或窗口(用于演示大致发生的情况):

 $( window ).bind('keypress', function(e){
   if ( $( e.originalTarget ).is( ':input' ) && e.keyCode == 13 ) {
     $(  e.originalTarget )
       .closest( 'form' )
         .find( 'input[type=submit]:first' )
           .click();
   }
 });

当然假设尚未在该事件上调用 .preventDefault()

底线:如果你有事件,你可以从中推测它来自什么元素,从而推断它属于哪种形式。 即使在这种情况下:

<input type="button" value="LOGIN" name="btnLoginOk" onclick="submit();">

这里的 submit() 是一个全局函数,但是当它被调用时,它的上下文 (this) 将是元素,你可以这样做 提交(e){ this.form.submit(); }

该应用程序的设计没有提交按钮(如输入
type="submit"),相反,设计者采用了 onclick 处理。

在我看来,设计师并没有完全理解 DOM / 表单事件,并且正在绕着问题走很长的路。 另一个可能的原因可能是该程序很旧,并且是在这些东西不像今天那样稳定或没有正确记录的时候设计的。

替换为: 替换

<form action="/login/" method="POST">
  [...]
  <input type="button" value="LOGIN" name="btnLoginOk" onclick="submit();">
</form>

为:

<form action="/login/" method="POST">
  [...]
  <input type="submit" value="LOGIN" name="btnLoginOk">
</form>

然后向所有需要它的表单添加一个键处理程序,该处理程序会在满足某些条件时检测并抑制输入(对于您实际上想要禁用此功能的表单)。

// for all forms that POST that have 2+ submit buttons
$( 'form[method=post]:has(:submit:eq(1))' ).bind('keydown', function(e){
  // if target is an enter key, input element, and not a button
  if ( e.keyCode == 13 && e.target.tagName == 'INPUT' && 
       !/^(button|reset|submit)$/i.test( e.target.type ) ) {
    return false;  // kill event
  }
});

或者更好的是:使用知道如何为您执行此操作的表单验证库(或 jQuery 插件)。

page contains multiple forms, and the
application would not be able to
determine (or, so I am told) which
form to action.

When you press enter on an input control the browser seeks the first submit button in that form and simulates a click on it. In the case of multiple buttons, the first one will be pressed on keyboard enter (this is by no means written in stone and browsers may deviate from this).

If you have two forms, the one that got a keypress will have it's first submit button pressed. Therefore you don't really need any special handling of this. You just have to stop being in the way.

You can simulate this in code, on a form:

 $( 'form' ).bind('keypress', function(e){
   if ( e.keyCode == 13 ) {
     $( this ).find( 'input[type=submit]:first' ).click();
   }
 });

Or window (for a demonstration of what is roughly happening):

 $( window ).bind('keypress', function(e){
   if ( $( e.originalTarget ).is( ':input' ) && e.keyCode == 13 ) {
     $(  e.originalTarget )
       .closest( 'form' )
         .find( 'input[type=submit]:first' )
           .click();
   }
 });

Assuming of course that .preventDefault() has not been called on the event.

Bottom line: If you have the event you can divine from it what element it came from and therefore which form it belongs to. Even in this case:

<input type="button" value="LOGIN" name="btnLoginOk" onclick="submit();">

Here submit() is a global function, but when it is called, its context (this) will be the element and you may do submit(e){ this.form.submit(); }.

The application has been designed so there are no submit buttons (as in input
type="submit") and, instead, the designers have gone for onclick handling.

This sounds to me like the designer doesn't fully comprehend DOM / form events and is going the long way around the problem. Another likely reason could be that the program is old and was designed back when these things weren't quite as stable, or properly documented, as they are today.

Replace this:

<form action="/login/" method="POST">
  [...]
  <input type="button" value="LOGIN" name="btnLoginOk" onclick="submit();">
</form>

With this:

<form action="/login/" method="POST">
  [...]
  <input type="submit" value="LOGIN" name="btnLoginOk">
</form>

Then add a key handler to all forms that need it, that detects and suppresses enter if some condition is met (for the forms that you actually do want to disable this on).

// for all forms that POST that have 2+ submit buttons
$( 'form[method=post]:has(:submit:eq(1))' ).bind('keydown', function(e){
  // if target is an enter key, input element, and not a button
  if ( e.keyCode == 13 && e.target.tagName == 'INPUT' && 
       !/^(button|reset|submit)$/i.test( e.target.type ) ) {
    return false;  // kill event
  }
});

Or better still: Use a form validation library (or jQuery plugin) that knows how to do this for you.

风尘浪孓 2024-07-23 18:23:55

您可以在 jquery 中为 Enter 键绑定按键事件:

$("form").bind("keypress", function(e) {
   if (e.keyCode == 13) {
      your_custom_submit_function();
      return false; // ignore the default event
   }
});

You can bind key press event for Enter key in jquery :

$("form").bind("keypress", function(e) {
   if (e.keyCode == 13) {
      your_custom_submit_function();
      return false; // ignore the default event
   }
});
审判长 2024-07-23 18:23:55

手指交叉,这会起作用

$(document).ready(function() {
  var focussed_form;
  $('input').focus(focus_form);
  $('input').blur(unfocus_form);
  $('textarea').focus(focus_form);
  $('textarea').blur(unfocus_form);
  $('select').focus(focus_form);
  $('select').blur(unfocus_form);

  $(document).keypress(function(e) {
    if(e.keyCode == 13) 
    {
      $(focussed_form).trigger('submit'); 
    }
  });

  function focus_form(){
    focussed_form = $(this).closest('form');
  }

  function unfocus_form(){
    focussed_form = NULL;
  }

});

Fingers crossed this'll work

$(document).ready(function() {
  var focussed_form;
  $('input').focus(focus_form);
  $('input').blur(unfocus_form);
  $('textarea').focus(focus_form);
  $('textarea').blur(unfocus_form);
  $('select').focus(focus_form);
  $('select').blur(unfocus_form);

  $(document).keypress(function(e) {
    if(e.keyCode == 13) 
    {
      $(focussed_form).trigger('submit'); 
    }
  });

  function focus_form(){
    focussed_form = $(this).closest('form');
  }

  function unfocus_form(){
    focussed_form = NULL;
  }

});
尸血腥色 2024-07-23 18:23:55

我认为你不需要 jQuery 或 javascript 来做到这一点。 您确实可以检测用户点击了哪个表单提交,甚至点击了哪个提交按钮(如果每个表单有多个提交按钮)。

下面是您将使用的代码:

<form action="http://foo.com/uri" method="POST">
  <input type="submit" name="action1a" value="add" />
  <input type="submit" name="action1b" value="delete" />
</form>
<form action="http://foo.com/uri" method="POST">
  <input type="submit" name="action2a" value="add" />
  <input type="submit" name="action2b" value="delete" />
  <input type="submit" name="action2c" value="someOtherAction" />
</form>

然后,对于表单处理脚本,您只需检查提交按钮的名称,例如:

if (isset($_POST['action1a'])) ... // PHP syntax

或者,您可以使用图像作为提交按钮,然后您可以对所有提交使用相同的名称按钮并检查它们的值 - 如果每个按钮都有唯一的 value 属性,您也可以使用普通的提交按钮执行此操作。


Edit:
One more method is to combine POST and GET values, i.e.:

<form action="http://foo.com/uri?form=1" method="POST">
  <input type="submit" name="action" value="add" />
  <input type="submit" name="action" value="delete" />
</form>
<form action="http://foo.com/uri?form=2" method="POST">
  <input type="submit" name="action" value="add" />
  <input type="submit" name="action" value="delete" />
</form>

I don't think you need jQuery or javascript for this. You can indeed detect which Form a user hits submit on, or even which submit button is hit (in case there are more than one submit button per form).

Here's the code you would use:

<form action="http://foo.com/uri" method="POST">
  <input type="submit" name="action1a" value="add" />
  <input type="submit" name="action1b" value="delete" />
</form>
<form action="http://foo.com/uri" method="POST">
  <input type="submit" name="action2a" value="add" />
  <input type="submit" name="action2b" value="delete" />
  <input type="submit" name="action2c" value="someOtherAction" />
</form>

Then for the form-processing script, you would simply check the name of the submit button, for example:

if (isset($_POST['action1a'])) ... // PHP syntax

Alternatively, you can use images as submit buttons, then you can use the same name for all the submit buttons and just check their values--you can also do this with normal submit buttons if every button has a unique value attribute.


Edit:
One more method is to combine POST and GET values, i.e.:

<form action="http://foo.com/uri?form=1" method="POST">
  <input type="submit" name="action" value="add" />
  <input type="submit" name="action" value="delete" />
</form>
<form action="http://foo.com/uri?form=2" method="POST">
  <input type="submit" name="action" value="add" />
  <input type="submit" name="action" value="delete" />
</form>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文