繁体   English   中英

Knockoutjs不会更新ViewModel

[英]Knockoutjs does not update viewmodel

在我看来,我有文件输入:

<input type="file" class="file-input" name="file_source" size="40"  onchange=''>

以及跨度,其中显示了上传的文件名:

<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();
        });

该范围与敲除js绑定在一起:

viewModel = {
image: ko.observable()
}

ko.applyBindings(viewModel);

问题是,当我更新跨度文本时,可观察对象不会更新。 尽管我在跨度中有文件名,但是可观察的是空的。 跨度文本更改时,如何使可观察对象进行自我更新?

根据对您的问题的评论,我做了个小提琴 这应该工作:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM