使用knockoutjs时,为什么我的选择列表选择选项没有被设置?

时间:2012-06-13 15:23:25

标签: knockout.js

我能够成功设置选择列表的选项,但未设置所选选项。

谁能看到我做错了什么?

http://jsfiddle.net/devlife/YmshY/2/

HTML:

<select data-bind="options: FundTypeOptions, optionsText: 'Text', optionsValue: 'Value', value: 

Javscript:

$root.SelectedFundType"></select>​

$(function(){

    var options = {
        "ImportType":0,

        "ImportTypeList":{
            "":"",
            "Funds":"Fund Level Investments",
            "Valuations":"Valuations"
        },

        "FundTypeOptions":[
            {"Value":"","Text":""},
            {"Value":"Initial","Text":"Initial"},
            {"Value":"FollowOn","Text":"Follow-on"}
        ],

        "SelectedImportType":"Funds",

        SelectedFundType: "FollowOn"
    };

    var viewModel = {

        FundTypeOptions: ko.observableArray(options.FundTypeOptions)

    };

    ko.applyBindings(viewModel);
});​

1 个答案:

答案 0 :(得分:1)

您的SelectedFundType不是viewModel的属性。

var viewModel = {

    FundTypeOptions: ko.observableArray(options.FundTypeOptions),
    SelectedFundType: ko.observable("FollowOn")
};

http://jsfiddle.net/h8mfK/1/

希望这有帮助。