在teleric autocomplete中下拉列表值和文本

时间:2016-04-24 08:39:48

标签: jquery telerik

我有一个下拉列表,其中包含来自teleric UI的简单自动完成。我只想通过jQuery从下拉列表中获取文本和值。但是它不起作用。 这是代码

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta charset="utf-8" />
    <title>Kendo UI Snippet</title>
    <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2015.3.1111/styles/kendo.common.min.css">
    <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2015.3.1111/styles/kendo.rtl.min.css">
    <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.1.412/styles/kendo.silver.min.css" />
    <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2015.3.1111/styles/kendo.mobile.all.min.css">
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script src="http://kendo.cdn.telerik.com/2015.3.1111/js/kendo.all.min.js"></script>
    <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2015.3.1111/styles/kendo.default.min.css">
    <script src="http://kendo.cdn.telerik.com/2015.3.1111/js/angular.min.js"></script>
    <script src="http://kendo.cdn.telerik.com/2015.3.1111/js/jszip.min.js"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <%--<input id="autocomplete" />
 --%>       <<asp:DropDownList ID="autocomplete" runat="server" ClientIDMode="Static">
        </asp:DropDownList>

    </div>
    </form>
    <script type="text/javascript">
        $(function () {
            var data = [
                { id: 1, name: "Apples" },
                { id: 2, name: "Oranges" }
            ];
            $("#autocomplete").kendoAutoComplete({
                dataTextField: "name", // The widget is bound to the "name" field
                dataSource: data
            });
            $('#autocomplete').on('change', function() {
                var autoCompleteValue = $('#autocomplete').val();
                var autoCompleteText = $('#autocomplete').text();
                alert(autoCompleteValue + '->' + autoCompleteText);
            });

        });
    </script>
</body>
</html>

我希望在更改下拉列表时获取价值和文字。我选择苹果

我想要

autoCompleteValue = "1";
autoCompleteText = "Apples";

2 个答案:

答案 0 :(得分:0)

您可以稍微修改代码以实现解决方案:

<input id="autocomplete" />
<div id="result"></div>

$("#autocomplete").kendoAutoComplete({
    dataTextField: "name",
    select: function(e) {
        var dataItem = this.dataItem(e.item.index());            
        //output selected dataItem
        $("#result").html(kendo.stringify(dataItem)); // Bind html to see your selected text and value      
    },
    dataSource: {
        data: [
            { name: "Apples", value: 1 },
            { name: "Oranges", value: 2 },
            { name: "Bananas", value: 3 },
            { name: "Mangos", value: 4 }
        ]
    }
});

答案 1 :(得分:0)

您可能需要更改数据源自动填充功能,如下所示:

var items = new kendo.data.DataSource({
    transport: {
        read: {
            url     : "/Home/GetItems",
            type    : "POST",
            dataType: "json"
        }
    },
    schema   : {
        model: {
            id    : "id",
            fields: {
                id  : { type: "id" },
                name: { type: "string" }
            }
        }
    }
});

var autoComplete = $("#autocomplete").kendoAutoComplete({
    minLength      : 3,
    separator      : ", ",
    dataSource     : items,
    serverFiltering: true,
    dataTextField  : "name"
}).data("kendoAutoComplete");