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

javascript - get selected option text in knockout - Stack Overflow

programmeradmin0浏览0评论

I am using knockoutjs to bind a select list. Here is a Sample , I want to get selected option text instead of selected value.

How to get it using knockoutjs ?

<select id="projectMenu" name="projectMenu" data-bind="   
        value: selectedProject,
        options:        projectFilters,
        optionsText:    'a', 
        optionsValue:   'b',   
        optionsCaption: '-- Select Project --'
    ">
    </select>
<b>Selected Project:</b> <span data-bind="text: selectedProject"></span>

I am using knockoutjs to bind a select list. Here is a Sample , I want to get selected option text instead of selected value.

How to get it using knockoutjs ?

<select id="projectMenu" name="projectMenu" data-bind="   
        value: selectedProject,
        options:        projectFilters,
        optionsText:    'a', 
        optionsValue:   'b',   
        optionsCaption: '-- Select Project --'
    ">
    </select>
<b>Selected Project:</b> <span data-bind="text: selectedProject"></span>
Share Improve this question edited Oct 24, 2013 at 10:53 Damien 8,9873 gold badges33 silver badges43 bronze badges asked Oct 24, 2013 at 9:53 Sudarshan TanwarSudarshan Tanwar 3,6073 gold badges27 silver badges39 bronze badges 3
  • please provide a code example, not simply a link. – dokaspar Commented Oct 24, 2013 at 9:59
  • 2 Possible duplicate? stackoverflow.com/questions/11112435/… – Henk Jansen Commented Oct 24, 2013 at 9:59
  • 2 if duplicate then what?? that question never been answered, does it mean no body can ever ask? – sairfan Commented Oct 24, 2018 at 19:44
Add a comment  | 

3 Answers 3

Reset to default 10

The simplest way to do it is to remove the optionsValue binding. When you don't sepcify the optionsValue binding, the entire item will be the selected value.

<select id="projectMenu" name="projectMenu" data-bind="   
        value: selectedProject,
        options:        projectFilters,
        optionsText:    'a',         
        optionsCaption: '-- Select Project --'
    ">
    </select>
<b>Selected Project:
<span data-bind="text: selectedProject() ? selectedProject().a : 'no selection '"></span>

See fiddle

As far I am concerned it is not possible with just a simple binding. But You can easily create computedObservable which choose optionText based on optionValue

vm.selectedOption= ko.computed(function () { 
   for (var i = 0; i < this.projectFilters().length; i += 1) {
       var data = this.projectFilters()[i];
       if (data.a === this.selectedProject()) {
           return data.b;
       }
   }
   return null;
}, vm);
vm.selectedCountryName = ko.computed(function () {
        var text = '';
        ko.utils.arrayForEach(vm.countries(), function (item) {
            if (item.CountryId == vm.selectedCountry()) {
                text = item.CountryName;
                return;
            }
        });
        return text;
    });
发布评论

评论列表(0)

  1. 暂无评论