简体   繁体   中英

Enter key to submit form immediately

edit
This question is different than the suggested duplicate in that it uses the form element and the event submit form where the suggested duplicate does not.

This Meteor client code needs to fire the submit form event once Enter key is pressed, but instead it moves the focus to the next input element on the screen till it reaches the last element then it fires the submit, which is not what's needed, rather submit immediately.
How can it be fixed? thx

  Template.content.events({
  'submit form': function (event) {
    event.preventDefault();
    concole.log('form submitted');
  }
});
<template name="content">
  <form>
    {{#each this.items}} {{> sub}} {{/each}}
  </form>
</template>


<template name="sub">
  {{#if isEqual element "input"}} {{> input}} {{/if}}
</template>
<template name="input">
  <input class={{class}} type="{{#if type}}{{type}}{{else}}text{{/if}}" name={{name}} placeholder={{placeholder}} value={{value}} data-id={{_id}}>
</template>

There are two to solve this problem.

  1. 'type="submit"': = You can set type = submit to your all input fields.
  2. Call submit method when user press enter key on input fields.

     Template.content.events = { 'keypress input': function (evt, template) { if (evt.which === 13) { $( "form" ).submit(); } }, 'submit form': function (event) { event.preventDefault(); concole.log('form submitted'); } }; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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