简体   繁体   English

单击Enter键时检测到提交事件

[英]detect the submit event when click enter key

If I have one form with actually different inputs for two submit requests. 如果我有一个表单,其中两个提交请求的输入实际上不同。 When I click on any submit button I can know what action to do. 当我单击任何提交按钮时,我可以知道要执行的操作。 but I need to detect in which input I'm when click Enter keyboard key. 但是单击输入键盘键时,我需要检测输入的内容。

<form class="main-form">
  <div class="form_one">
    <input class="form_one_input" type="text" id="form_one_input"/>
    <button type="submit" class="form_one_button">Submit form one</button>
  </div>
  <div class="form_two">
    <input class="form_two_input" type="text" id="form_two_input"/>
    <button type="submit" class="form_two_button">Submit form two</button>
  </div>
</form>

https://jsfiddle.net/m6433obp/ https://jsfiddle.net/m6433obp/

To detect which input you are in use the keyup event handler as: 要检测您正在使用哪个输入,请使用keyup事件处理程序,如下所示:

$('.form_one').bind('keyup', function(e) {
    if(e.keyCode === 13) {
    alert($(this).attr('class'));
  }
})
$('.form_two').bind('keyup', function(e) {
    if(e.keyCode === 13) {
    alert($(this).attr('class'));
  }
})

check demo for this here 在这里检查演示

I am not really sure that I understand your question correctly. 我不确定我是否正确理解您的问题。 But I think that since you are using two submit buttons in the same form, you should give unique names to the buttons, so that on the server side you know that on hitting enter which input got submitted. 但我认为,由于您使用的是同一格式的两个提交按钮,因此应为按钮指定唯一的名称,以便在服务器端您知道单击输入即提交了输入。

<form class="main-form">
  <div class="form_one">
    <input class="form_one_input" type="text" id="form_one_input"/>
    <button type="submit" name="first_button" class="form_one_button">Submit form one</button>
  </div>
  <div class="form_two">
    <input class="form_two_input" type="text" id="form_two_input"/>
    <button type="submit" name="second_button" class="form_two_button">Submit form two</button>
  </div>
</form>

And the code to check: 并检查代码:

 if (isset($_POST['first_button'])) {

    } else if (isset($_POST['second_button'])) {

    } else {        
       //no button pressed
}

Hope this is what you were looking for. 希望这就是您想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM