Knockoutjs清除组合框中的选定值 [英] Knockoutjs clear selected value in combobox

查看:87
本文介绍了Knockoutjs清除组合框中的选定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个简单的knockout.js应用程序:

I have this simple knockout.js application:

查看:

<select data-bind="options: allDocumentTypes , optionsCaption: 'Choose ...', optionsValue: 'id', optionsText: 'name', selectedOptions: selectedDocument"></select>
<span data-bind="click: cl">CLEAR VALUE!</span>

这个简单的ViewModel:

and this simple ViewModel:

function documentType(id, name){
    this.id = id;
    this.name = name;
}

var viewModel = {
    allDocumentTypes: ko.observableArray([]),
    selectedDocument: ko.observable(''),
    cl: function(){
       viewModel.selectedDocument('');
    }
};

/* load data */
viewModel.allDocumentTypes.push(new documentType(1,'Test 1'));
viewModel.allDocumentTypes.push(new documentType(2,'Test 2'));
ko.applyBindings(viewModel);

我希望,在点击spanCLEAR VALUE!之后,将选择select选项选择......,但它没有发生。
viewModel中的值设置为(空字符串),这是正确的,但用户仍然在select中看到旧值。

I would expect, that after i click on span "CLEAR VALUE!", in select will be selected option "choose...", but it is not happening. The value in viewModel is set to "" (empty string), which is right, but user still see old value in select.

有没有办法做到这一点?

Is there any way to do that?

感谢您的帮助:)

推荐答案

您必须更改绑定类型值而不是selectedOptions。下一步是在cl函数中设置viewModel.selectedDocument:

You must change binding type to "value" instead of "selectedOptions". Next step is to set viewModel.selectedDocument in cl function:

viewModel.selectedDocument(null);

这篇关于Knockoutjs清除组合框中的选定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆