本文介绍了击倒剑道下拉列表获取所选项目的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序是MVC5.我正在使用下面的Knockout Kendo下拉列表:

My application is MVC 5. I am using the following Knockout Kendo dropdownlist:

     <input data-bind="kendoDropDownList: { dataTextField: 'name',
 dataValueField: 'id', data: foodgroups, value: foodgroup }" />
         <hr />
         Selected: <strong data-bind="text: foodgroup"> </strong>

    <script>
        var ViewModel = function () {
            var self = this;
            this.foodgroups = ko.observableArray([
                { id: "1", name: "apple" },
                { id: "2", name: "orange" },
                { id: "3", name: "banana" }
            ]);
            var foodgroup =
            {
                name: self.name,
                id: self.id
            };
            this.foodgroup = ko.observable();
            ko.bindingHandlers.kendoDropDownList.options.optionLabel = " - Select -";
            this.foodgroup.subscribe(function (newValue) {
                alert(newValue.name);
            });
        };
        ko.applyBindings(new ViewModel());
    </script>

我正在尝试获取所选项目的文本.如果我使用alert(newValue),则获得ID,而当我使用newValue.name或newValue.Text时,则得到未定义.

I am trying to get the text of the selected item. If I use alert(newValue) I get the id, when I use newValue.name or newValue.Text I get undefined.

推荐答案

我认为KendoDropDownList()不支持将复杂对象作为数据值.然后,我认为更好的方法是使用ko.utils.arrayFirst().

I think that KendoDropDownList() does not support having a complex object as a data value.Then I think the better approach is using ko.utils.arrayFirst().

为了方便起见,我做了一个 Jsfiddle示例

For convinience I did an Jsfiddle example

希望这项帮助

这篇关于击倒剑道下拉列表获取所选项目的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 04:46