Kendo UI下拉列表更改触发事件

4

我第一次使用Kendo UI,想在下拉列表改变时触发一个函数,但是遇到了一些困难。

我的目标是根据用户的下拉选择显示不同的搜索字段。我已经尝试了几种方法,但似乎都没有起作用。

有没有简单的jQuery代码片段可以获取Kendo UI下拉列表的文本?

我的代码如下:

  <script>
    $(document).ready(function () {
        var a = $("div#searchbox span.k-input").text();
        console.log(a);
      $(a).change(function(){
            $('.searchingfor').hide();
            $('#' + a).show();
        });
    });
</script>
 @using (Html.BeginForm())
{ 
    <div id="searchbox" class="label">
        @Html.Label("Search")
        @Html.TextBox("QuickSearch", null, new { style = "width:91%", @class = "k-input" })
        <br />
        <br />
        @(Html.Kendo().DropDownList()
                        .DataTextField("Text")
                        .DataValueField("Value")
                        .BindTo(new List<SelectListItem>()
                        {
                            new SelectListItem()
                            {
                                Text = "All",
                                Value = "1"
                            },
                            new SelectListItem()
                            {
                                Text = "Customer",
                                Value = "2"
                            },
                            new SelectListItem()
                            {
                                Text = "Contact",
                                Value = "3"
                            },
                            new SelectListItem()
                            {
                                Text = "Service Employee",
                                Value = "4"
                            },
                            new SelectListItem()
                            {
                                Text = "Organization",
                                Value = "5"
                            },
                            new SelectListItem()
                            {
                                Text = "Employee",
                                Value = "6"
                            },
                            new SelectListItem()
                            {
                                Text = "Other",
                                Value = "7"
                            }
                        })
                   .Name("SearchType")
                    )
    </div>
}
3个回答

7
@(Html.Kendo().DropDownList()
                    .DataTextField("Text")
                    .DataValueField("Value")
                    .BindTo(new List<SelectListItem>()
                    {
                        new SelectListItem()
                        {
                            Text = "All",
                            Value = "1"
                        },
                        new SelectListItem()
                        {
                            Text = "Customer",
                            Value = "2"
                        },
                        new SelectListItem()
                        {
                            Text = "Contact",
                            Value = "3"
                        },
                        new SelectListItem()
                        {
                            Text = "Service Employee",
                            Value = "4"
                        },
                        new SelectListItem()
                        {
                            Text = "Organization",
                            Value = "5"
                        },
                        new SelectListItem()
                        {
                            Text = "Employee",
                            Value = "6"
                        },
                        new SelectListItem()
                        {
                            Text = "Other",
                            Value = "7"
                        }
                    })
               .Name("SearchType")
               .Events(e => e.Change("OnSearchTypeChange"));

<script type="text/javascript">
function OnSearchTypeChange(e)
{
 //Do whatever you want to do
}
</script>

1

订阅onSelect事件,然后获取所选项目的文本。以下来自kendo演示网站。

    function onSelect(e) {
                    if ("kendoConsole" in window) {
                        var dataItem = this.dataItem(e.item.index());
                        kendoConsole.log("event :: select (" + dataItem.text + " : " + dataItem.value + ")" );
                    }
                };

0

我使用Kendo MVC,我的下拉列表代码如下:

@(Html.Kendo()
    .DropDownListFor(p=> p.SelectedItem)
    .BindTo((List<SelectListItem>)ViewBag.SelectedListItems)
    .Events(p => p.Change("function(e){list_change(e);}")
))

所以在更改函数中:

function personType_Change(e) {
    var item = $('#SelectedItem').data("kendoDropDownList");

    //use item.value() and write your own codes

}

也许能帮助到某些人 :)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接