[英]Knockoutjs does not update viewmodel
In my view I have file input: 在我看来,我有文件输入:
<input type="file" class="file-input" name="file_source" size="40" onchange=''>
And span, in which I am showing the uploaded filename: 以及跨度,其中显示了上传的文件名:
<span class='label label-info' id="upload-file-info" data-bind="text: image"></span>
$(".file-input").change(function() {
var elem = $("#upload-file-info");
elem.html = $(this).val();
});
This span is binded with knockoutjs: 该范围与敲除js绑定在一起:
viewModel = {
image: ko.observable()
}
ko.applyBindings(viewModel);
The problem is that the observable do not updates when I update the span text. 问题是,当我更新跨度文本时,可观察对象不会更新。 Althought I have the filename in the span, the observable is empty.
尽管我在跨度中有文件名,但是可观察的是空的。 How can I make the observable to update itself when the span text changes ?
跨度文本更改时,如何使可观察对象进行自我更新?
I did a quick fiddle according to my comment on your question. 根据对您的问题的评论,我做了个小提琴 。 This should work:
这应该工作:
jQuery(document).ready(function ($) {
'use strict';
$(".file-input").change(function () {
var elem = $("#upload-file-info");
viewModel.image($(this).val());
});
var viewModel = {
image: ko.observable()
};
viewModel.image.subscribe(function (value) {
alert(value);
});
ko.applyBindings(viewModel);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.