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

问题描述

如何从下拉列表中获取所选名称的 id.
whene select Apples then got id 1and select Oranges then 2.
这是一个简单的剑道下拉示例.

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

<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>

谢谢.

推荐答案

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

In order to retrieve the selected Id you can use the dataItem object and access the id within it with change event:

 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);

工作示例

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

HTML

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

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);
                  };

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

08-26 04:37