【发布时间】:2014-07-24 15:57:21
【问题描述】:
我需要在按下 Enter 键时提交更改,而不是输入换行符。我正在使用 Ember.js 和稍微定制的 TextArea 助手。
这是我的模板中的内容
{{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()
The real problem is that when text selected and user types enter key to accept, it also types a newline which replaces all content in textarea, hence the only newline gets accepted.
如何防止输入新行,但触发事件以接受更改?
【问题讨论】:
-
TextArea 设计为多行。如果您只想支持单行,为什么不直接使用
<input type="text" />我们可以了解一下您为什么要这样做,这是非常规的吗? -
@Scott 问题是字符串可能很长,我需要能够显示 TextArea 的所有内容,使其更高,这是 TextField 无法做到的。或者可能?
-
有什么办法可以在
acceptChanges中使用preventDefault?
标签: javascript jquery ember.js