Javascript 在EmberJS的文本区域中按enter键时不要键入换行符
我需要在按下Enter键时提交更改,而不是键入换行符。我使用的是Ember.js和稍微定制的TextArea助手 这是我的模板中的内容Javascript 在EmberJS的文本区域中按enter键时不要键入换行符,javascript,jquery,ember.js,Javascript,Jquery,Ember.js,我需要在按下Enter键时提交更改,而不是键入换行符。我使用的是Ember.js和稍微定制的TextArea助手 这是我的模板中的内容 {{edit-item placeholder="Weight" value=weight insert-newline="acceptChanges" focus-out="acceptChanges"}} 在我的助手 App.EditItemView = Em.TextArea.extend didInsertElement: -> t
{{edit-item placeholder="Weight" value=weight insert-newline="acceptChanges" focus-out="acceptChanges"}}
在我的助手
App.EditItemView = Em.TextArea.extend
didInsertElement: ->
this.$().focus()
this.$().blur()
focusIn: ->
$this = this.$()
$this.get(0).select()
# fix webkit select problems
mouseUpHandler = ->
$this.off("mouseup", mouseUpHandler)
return false
$this.mouseup(mouseUpHandler)
attributeBindings: ['style', 'placeholder']
Em.Handlebars.helper 'edit-item', App.EditItemView
在我的acceptChagnges
操作中
# In controller action hook up
acceptChanges: ->
$(document.activeElement).blur()
@get('model').save()
真正的问题是,当选中文本并且用户输入key接受时,它也会输入一个换行符来替换textarea中的所有内容,因此唯一的换行符会被接受
如何防止键入新行,但触发事件以接受更改?我最近也不得不这么做,结果证明这相当简单。下面是一个相当简单的组件,它可以做到这一点:
App.NoteBoxComponent = Ember.TextArea.extend({
keyDown: function (event) {
if (event.which === 13 && ! event.shiftKey) {
// Don't insert newlines when submitting with enter
event.preventDefault();
}
},
// This next bit lets you add newlines with shift+enter without submitting
insertNewline: function (event) {
if (! event.shiftKey) {
// Do not trigger the "submit on enter" action if the user presses
// SHIFT+ENTER, because that should just insert a new line
this._super(event);
}
}
});
要使用它,只需将
{{note box action='foo'}}
添加到您的把手的某个位置。当用户点击enter键时,将触发操作“foo”。文本区域设计为多行。如果你只想支持一行,为什么不直接使用
我们可以了解一下你为什么要这样做,这是非常不传统的吗?@Scott问题是字符串可能很长,我需要能够显示文本区域的所有内容,使其更高,这在TextField中是不可能的。或者可能?是否有任何方法可用于接受更改
?