admin管理员组文章数量:1406937
Is it possible in Knockout to have a "live" binding between a text area and a DIV on the page that updates the DIV every time the content of the textarea changes (character per character)? I am using a puted field on my view model, but it won't update the DIV unless I tab off the textarea: Is it possible to update it instantly every time a change is made, without having to tab off?
Code
function EditModel() {
this.CommentTextPlain = ko.observable("");
var self = this;
this.CommentReady = koputed(function () {
return self.CommentTextPlain().replace(regex, "<BR>");
});
}
function ApplyViewmodel() {
model = new EditModel();
ko.applyBindings(model, document.getElementById("mainContainer"));
}
<div id="mainContainer">
<div id="target" data-bind='html: CommentReady' class="mentEditBox"></div>
<textarea data-bind="value: CommentTextPlain" rows="20" cols="62" id="editBoxFull"> </textarea>
</div>
Is it possible in Knockout to have a "live" binding between a text area and a DIV on the page that updates the DIV every time the content of the textarea changes (character per character)? I am using a puted field on my view model, but it won't update the DIV unless I tab off the textarea: Is it possible to update it instantly every time a change is made, without having to tab off?
Code
function EditModel() {
this.CommentTextPlain = ko.observable("");
var self = this;
this.CommentReady = ko.puted(function () {
return self.CommentTextPlain().replace(regex, "<BR>");
});
}
function ApplyViewmodel() {
model = new EditModel();
ko.applyBindings(model, document.getElementById("mainContainer"));
}
<div id="mainContainer">
<div id="target" data-bind='html: CommentReady' class="mentEditBox"></div>
<textarea data-bind="value: CommentTextPlain" rows="20" cols="62" id="editBoxFull"> </textarea>
</div>
Share
asked Mar 7, 2013 at 18:59
TGHTGH
39.3k12 gold badges105 silver badges140 bronze badges
3 Answers
Reset to default 6The value
binding has a panion option called valueUpdate
that you can set to include additional events like:
data-bind="value: CommentTextPlain, valueUpdate: 'afterkeydown'"
use valueUpdate
binding
see Additional Parameters section on http://knockoutjs./documentation/value-binding.html
valueUpdate
If your binding also includes a parameter called valueUpdate, this defines additional browser events KO should use to detect changes besides the change event. The following string values are the most monly useful choices:
- "keyup" - updates your view model when the user releases a key
- "keypress" - updates your view model when the user has typed a key. Unlike keyup, this updates repeatedly while the user holds a key down
- "afterkeydown" - updates your view model as soon as the user begins typing a character. This works by catching the browser’s keydown event and handling the event asynchronously. Of these options,
- "afterkeydown" is the best choice if you want to keep your view model updated in real-time.
Of these options, "afterkeydown" is the best choice if you want to keep your view model updated in real-time
Knockout has a textInput
binding that achieves immediate model updates without having to use additional parameters to the value binding, while also handling cut-and-paste and text drag events.
Here's the link to the textInput binding documentation: http://knockoutjs./documentation/textinput-binding.html.
本文标签: javascriptKnockoutJS bind to div as I39m typing in textareaStack Overflow
版权声明:本文标题:javascript - KnockoutJS bind to div as I'm typing in textarea - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745053790a2639812.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论