Test of valueAllowUnset

by sukobuto

HTML

<p>
    国を選択:
    <select data-bind="options: countries,
                       optionsCaption: '-選択してください-',
                       value: selectedCountry,
                       valueAllowUnset: true
                       "></select>
</p>

<p>選択された国: <span data-bind="text: selectedCountry"></span> </p>

<button data-bind="click: changeCountry">強制変更</button>

JavaScript

function ViewModel() {
    var self = this;
    self.countries = ['日本','ボリビア','ニュージーランド'];
    self.selectedCountry = ko.observable('ラトビア');
    self.changeCountry = function() {
        self.selectedCountry('リトアニア');
    };
    self.selectedCountry.subscribe(function(newValue) {
        console.log(newValue);
    });
}

ko.applyBindings(new ViewModel());