您好我想使用ko从下拉列表的选定值中获取所选文本。
我的观点是:
<select id="country" data-bind="options: CountriesList ,optionsText: 'CountryName',optionsValue:'CountryId',value:CountryId,optionsCaption: 'Select Country..'"
国家名单将如下:CountryId = 1 CoutnryName =&#34;阿富汗&#34; CountryId = 2 CountryName =&#34;阿尔巴尼亚&#34; ......等等
我的模特:
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计算国家名称...我不知道是不是正确的方式......请帮助我某人......
答案 0 :(得分:0)
试试这个:
CountryName: function()
{
var country = this.CountriesList().filter(function(i)
{
return i.CountryId == stateModel.CountryId();
});
return country[0].CountryName;
}