javascript 获取剑道下拉值的选定ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18738299/
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 13:02:14  来源:igfitidea点击:

get selected id of kendo drop down value

javascripthtmldrop-down-menukendo-uikendo-dropdown

提问by ?????

how to get id of selected name from dropdown.
whene select Applesthen got id 1and select Orangesthen 2.
this is simple kendo dropdown example.

如何从下拉列表中获取所选名称的 id。
whene select Applesthen got id 1and select Orangesthen 2.
这是简单的剑道下拉示例。

<body>
            <input id="dropdownlist" />

            <script>
                $("#dropdownlist").kendoDropDownList({
                  dataSource: [
                    { id: 1, name: "Apples" },
                    { id: 2, name: "Oranges" }
                  ],
                  dataTextField: "name",
                  dataValueField: "id",
                  index: 1,
                  select: onSelect
                });

                function onSelect(e) {
                console.log(e);
                  };
            </script>
</body>

thanks.

谢谢。

回答by hutchonoid

In order to retrieve the selected Id you can use the dataItemobject and access the id within it with changeevent:

为了检索选定的 ID,您可以使用该dataItem对象并通过change事件访问其中的 ID :

 var dataItem = e.sender.dataItem();
 $('#id').text(dataItem.id);

This will get you access to any data within the object too:

这也将使您可以访问对象中的任何数据:

$('#name').text(dataItem.name);

Working example

工作示例

http://jsfiddle.net/ygBq8/1/

http://jsfiddle.net/ygBq8/1/

Html

html

<input id="dropdownlist" /><br/>
<span id="id" >Id</span><br/>
<span id="name" >Name</span><br/>

JavaScript

JavaScript

$("#dropdownlist").kendoDropDownList({
                  dataSource: [
                    { id: 1, name: "Apples" },
                    { id: 2, name: "Oranges" }
                  ],
                  dataTextField: "name",
                  dataValueField: "id",
                  index: 1,
                  change: onChange
                });

                function onChange(e) {
                   var dataItem = e.sender.dataItem();
                   $('#id').text(dataItem.id);
                   $('#name').text(dataItem.name);
                  };

回答by Robin Giltner

The Select event is a bit more difficult one to use, as that event fires before the item is selected.

Select 事件有点难以使用,因为该事件在项目被选择之前触发。

If you use the Change event, you should be able to get the dataItem with

如果您使用 Change 事件,您应该能够使用

this.dataSource.get(this.value())

See sample http://jsbin.com/OcOzIxI/2/edit

请参阅示例http://jsbin.com/OcOzIxI/2/edit

回答by sanme98

Please use this.dataItem()

请使用 this.dataItem()

function onSelect(e) {
    alert(this.dataItem().id);
    alert(this.dataItem().Name);
};

回答by Mahdi Ataollahi

To select ID of the selected item use:

要选择所选项目的 ID,请使用:

$("#dropdownlist").val()

And to select TEXT of the selected item use:

并选择所选项目的 TEXT 使用:

$("#dropdownlist").data("kendoDropDownList").text()