按钮单击以在knockout

时间:2015-06-18 06:49:59

标签: knockout.js

我目前正在研究淘汰赛中的选项绑定,绑定是根据我的要求为我做的一切。但目前我面临的问题是

  

如何将optionsCaption值再次设置为按钮上的Select Box   点击。

这是我的代码

 var clickMe = function(){
//Do Something 
    alert("I am working");
    //Want to enable optionsCaption Value again as current value in select box
};
var operator = ko.observable();
var vm = 
 {
     operators : [
{id: 0, name: 'addition'},
{id: 1, name: 'subtraction'},
{id: 2, name: 'division'},
{id: 3, name: 'multiplication'}
   ],
 operator:operator,
 clickMe: clickMe
}
 ko.applyBindings(vm);

HTML

<select data-bind="options: $root.operators, optionsText: 'name', optionsValue: 'id', value: $root.operator, optionsCaption: 'Select Text'"></select>
<button data-bind="click: clickMe">Enable Caption</button>

请通过编辑我的小提琴手来回答 Fiddler Link

1 个答案:

答案 0 :(得分:2)

您需要将operator属性设置为nullundefined,以便在您的选择中取回optionsCaption值:

var clickMe = function(){
//Do Something 
    alert("I am working");  
    vm.operator(undefined);
};

演示JSFiddle