挖空剑道下拉列表获取所选项目的文本

Knockout Kendo dropdownlist get text of selected item

本文关键字:选项 项目 文本 获取 下拉列表      更新时间:2023-09-26

我的应用程序是MVC 5。 我正在使用以下淘汰剑道下拉列表:

     <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 时,我会得到未定义。

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

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

希望这个帮助