5

我希望能够设置淘汰赛的价值,observablevaluea selectwith case-insensitivity。因此,在我下面的示例中,传递justin将设置selectedValueJustin.

不工作:jsfiddle

工作:jsfiddle

HTML

<select id="people" data-bind="options: peopleList, value: selectedPerson, optionsCaption: 'Choose...'">
</select>

JS

function MyViewModel(defaultPerson) {
    var self = this;
    self.selectedPerson = ko.observable(defaultPerson);
    self.peopleList = ko.observableArray(["Justin", "Sam", "Chris", "John"]);
}

$(function(){
    var person = 'justin';
    var viewModel = new MyViewModel(person);

    ko.applyBindings(viewModel);
});
4

2 回答 2

1

使用optionsValueoptionsText绑定以“按原样”呈现条目,同时保持值不区分大小写。

function MyViewModel(defaultPerson) {
    var self = this;
    var Person = function(name) {
        this.name = name;
        this.ciName = name.toLowerCase();
    };
    self.selectedPerson = ko.observable(defaultPerson);
    self.ciSelectedPerson = ko.computed(function() {
        return self.selectedPerson().toLowerCase();
    });
    self.peopleList = ko.observableArray(
        ["Justin", "Sam", "Chris", "John"].map(
            function(s) { return new Person(s) }));
}

标记:

<select id="people" data-bind="options: peopleList, optionsValue: 'ciName', optionsText: 'name', value: ciSelectedPerson, optionsCaption: 'Choose...'">
</select>
于 2013-08-09T11:10:10.133 回答
1

您可以自己渲染选项而不使用options绑定。这将允许您将值转换为小写:

<select id="people" data-bind="value: selectedPerson, optionsCaption: 'Choose...'">
    <option value=''>Choose...</option>
    <!-- ko foreach: peopleList -->
        <option data-bind='value: $data.toLowerCase(), text: $data'></option>
    <!-- /ko -->
</select>

这是工作的jsFiddle

于 2013-08-09T10:30:55.307 回答