最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to set the initial default value of dropdown in knockout js - Stack Overflow

programmeradmin0浏览0评论

Here is my code in View

<select data-bind="options:TypeInfo,optionsText: 'PrefName',
        optionsValue: 'PrefName',value: $data.selectedchoice,event:
        {change:gettrivialtable}" class="forms-dropdown"></select>

Here is my Js Code

viewModel = ko.mapping.fromJS(viewData);

self.TypeInfo = viewModel.TypeInfo;

self.selectedchoice = ko.observable('');

Note : viewData is a Json Pushed from model.PrefName is a property of the TypeInfo class. The value returned are 0:'x',1:'y',2:'select' in the same order I want 'select' to be the default value. Please help me out.

Here is my code in View

<select data-bind="options:TypeInfo,optionsText: 'PrefName',
        optionsValue: 'PrefName',value: $data.selectedchoice,event:
        {change:gettrivialtable}" class="forms-dropdown"></select>

Here is my Js Code

viewModel = ko.mapping.fromJS(viewData);

self.TypeInfo = viewModel.TypeInfo;

self.selectedchoice = ko.observable('');

Note : viewData is a Json Pushed from model.PrefName is a property of the TypeInfo class. The value returned are 0:'x',1:'y',2:'select' in the same order I want 'select' to be the default value. Please help me out.

Share Improve this question edited May 24, 2013 at 14:31 Dilip Nandakumar asked May 24, 2013 at 14:22 Dilip NandakumarDilip Nandakumar 3181 gold badge2 silver badges14 bronze badges 1
  • Please post your gettrivialtable function – Jacques Snyman Commented May 27, 2013 at 4:58
Add a ment  | 

2 Answers 2

Reset to default 4

you could set your selectedchoice with variable from TypeInfo like:

self.selectedchoice(self.TypeInfo()[0])

or very similar

If you define your self.selectedChoice observable as

self.selectedchoice = ko.observable('select');

the select list will default to that option.

发布评论

评论列表(0)

  1. 暂无评论