如何从ko中的选项值计算选项文本



嗨,我想使用ko从下拉列表的选定值中获取选定的文本。

我的观点是:

    <select id="country" data-bind="options: CountriesList ,optionsText: 'CountryName',optionsValue:'CountryId',value:CountryId,optionsCaption: 'Select Country..'"

countrylist将是这样的:CountryId=1 CountryName="阿富汗" CountryId=2 CountryName="阿尔巴尼亚"…所以在

我的模型:

       var stateModel = {
            StateId: ko.observable(0),
            StateName: ko.observable('').extend({ required: true }).extend({ pattern: { message: 'Enter only Text', params: '^[a-zA-Z ]*$'} }),
            ShortName: ko.observable('').extend({ required: true }).extend({ pattern: { message: 'Enter only Text', params: '^[a-zA-Z ]*$'} }),
            IsActive: ko.observable(true),
            CountryId: ko.observable().extend({ required: true }),
            CountriesList: ko.observableArray([]),
            CountryName: ko.computed(function () {
                return $("#country CountryId[value=111]").text();
            })
        }; 

我正在尝试从ko.computed计算国家名称…

试试这个:

CountryName: function()
     {
     var country = this.CountriesList().filter(function(i)
         {
         return i.CountryId == stateModel.CountryId();
         });
     return country[0].CountryName;
     }

最新更新