javascript Kendo UI Web - DropDownList:选择事件没有正确返回选定的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21592228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 21:15:53  来源:igfitidea点击:

Kendo UI Web - DropDownList: select event doesn't return selected value properly

javascriptjquerywebkendo-uikendo-dropdown

提问by Glenn Mohammad

I am binding a DropDownListwidget to selectevent like so:

我将一个DropDownList小部件绑定到select事件,如下所示:

var items = [
    { text: 'Item 3', value: '3' },
    { text: 'Item 4', value: '4' }
];

var dropDownListEl = $('#dropdownlist');
dropDownListEl.kendoDropDownList({
    dataTextField: 'text',
    dataValueField: 'value',
    index: 0
});

var kDropDownList = dropDownListEl.data('kendoDropDownList'),
    ds = kDropDownList.dataSource;

items.forEach(function (item) {
    ds.add(item);
});

kDropDownList.bind('select', function (e) {
    console.log('this.value(): ' + this.value());
});

But, it doesn't return the correct value when I do the selection.

但是,当我进行选择时,它不会返回正确的值。

I have been trying almost every possibility there is, none is working. http://jsfiddle.net/glenn/gxJ3S/

我一直在尝试几乎所有的可能性,都没有奏效。http://jsfiddle.net/glenn/gxJ3S/

It's driving me insane!

快把我逼疯了!

回答by Goddard

Binding Select Eventof Kendo DropDownListas follow to get correct selected item

绑定选择事件剑道的DropDownList如下得到正确选择的项目

   kDropDownList.bind('select', function (e) {
       var dataItem = this.dataItem(e.item.index());
        console.log('this.value(): ' + dataItem.value);

   });

Here is the working JSFiddle

这是工作中的 JSFiddle

回答by softawareblog.com

use changeevent instead, it's more straightforward

改用更改事件,它更直接

..
  change: function(e) {
    var value = this.value();
    // Use the value of the widget
  }
..

回答by Ihor Pavlyk

var _item = e.sender.dataItem(e.sender.selectedIndex);

回答by user1477388

I think Kendo changed their API:

我认为 Kendo 改变了他们的 API:

Important: Since version Q1 2015 (2015.1.318), the option label has been moved outside the item list DOM collection. As a result, jQuery.index() can no longer be used to reliably detect if the option label is the selected dropdown item.

重要提示:自 2015 年第一季度 (2015.1.318) 起,选项标签已移至项目列表 DOM 集合之外。因此,jQuery.index() 不能再用于可靠地检测选项标签是否是选定的下拉项。

Ref. http://docs.telerik.com/kendo-ui/api/javascript/ui/dropdownlist#events-select

参考 http://docs.telerik.com/kendo-ui/api/javascript/ui/dropdownlist#events-select

Ultimately, this is the only thing that worked for me:

最终,这是唯一对我有用的东西:

var item = e.sender.dataItem(e.item)

回答by Timea Emma

In case you use angular, you can get the selected item with: e.sender.dataItem(e.item.index())

如果您使用 angular,您可以通过以下方式获取所选项目: e.sender.dataItem(e.item.index())