我有一个可观察的..
this.mytextbox=ko.observable("Daily");
我希望删除按钮上的最后一个字符。
我尝试了以下-
this.removesinglechar=function(){
self.mytextbox().substring(0, self.mytextbox().length - 1);
}
也 self.mytextbox().slice(0, -1);
真诚的感谢您的帮助。
谢谢
您的方法效果很好,但是您忘记了实际设置可观察值。(使用self.mytextbox(theCodeThatReturnsTheNewString)
)
var VM = function() {
var self = this;
this.label = ko.observable("A long string");
this.removeLastChar = function() {
self.label(self.label().slice(0, self.label().length - 1));
};
};
ko.applyBindings(new VM());
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<div>
<span data-bind="text: label"></span><button data-bind="click:removeLastChar">-1</button>
</div>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句