在我将操作放入表单标签 ( <form {{action 'registerAccount'}}>
) 之后,我观察到如果当前输入字段失去焦点,表单总是会被提交。
如果这是有意为之的行为,我该如何避免?
App.AccountRegisterController = Ember.ObjectController.extend({
title: 'Registrieren',
content: [],
actions: {
registerAccount: function () {
var newAccount = this.get('store').createRecord('account', {
email: this.get('email'),
password: this.get('password'),
isNewsletterSubscribed: this.get('isNewsletterSubscribed')
});
newAccount.save();
}
}
});
编辑:
我的错。用这个替换前面提到的 Action 助手:
<form {{action 'registerAccount' on='submit'}}>
最佳答案
通过添加 <form {{action 'registerAccount'}}>
, registerAccount
每次在表单内单击都会调用函数,例如在输入字段内单击以输入文本时。
通过指定 DOM 事件类型 submit
通过使用 on
属性(property),registerAccount
应该为提交类型的点击事件调用函数,这意味着仅针对 type=submit
的输入或 button
type=submit
的元素默认情况下。
如果您可以发布您的 Handlebars 模板,这将有所帮助 AccountRegisterView
.
我建议将您的 {{action}}
按钮元素内的助手,即
<button {{action 'registerAccount'}}>submit form</button>
然后您的表单提交只会在您单击按钮时发生。
除非它是您想要实现的其他目标。
关于javascript - Ember.js 表单即时提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20856661/