如何在meteorjs模板中收听表单的提交事件?

Sui*_*oth 5 handlebars.js meteor

如果我有这样的模板

<template name="my_form">
  <form name="my_form">
    <input name=" ....
  </form>
</template>
Run Code Online (Sandbox Code Playgroud)

我想听听"my_form"的提交事件.

我试过这个:

Template.my_form.events({
  'submit form': function( event ){   // also tried just 'submit'
    console.log( 'Submitting form!' );
    event.preventDefault();
    event.stopPropagation();
    return false; 
  }
});
Run Code Online (Sandbox Code Playgroud)

但没有运气.事件处理程序似乎没有注册.有什么我想念的吗?

ps我知道我可以通过听取提交按钮单击事件来处理表单"提交",但我需要在此特定方案中使用表单提交事件.

Tob*_*old 7

似乎你没有遗漏某些东西.我无法重现你的问题.当textinput具有焦点时点击返回时,控制台会打印"提交表单!" 正如所料.

我的代码,只有两个文件:

form.html:

<head>
  <title>form</title>
</head>

<body>
  {{> my_form}}
</body>

<template name="my_form">
  <form name="my_form">
    <input></input>
  </form>
</template>
Run Code Online (Sandbox Code Playgroud)

form.js

if (Meteor.isClient) {
    Template.my_form.events({
      'submit form': function( event ){   // also tried just 'submit', both work for me!
        console.log( 'Submitting form!' );
        event.preventDefault();
        event.stopPropagation();
        return false; 
      }
    });
}
Run Code Online (Sandbox Code Playgroud)