简体   繁体   中英

Kendo UI DropDownList on change to trigger event

I'm using Kendo UI for the first time and am having some difficulty triggering a function on my Kendo dropdownlist change.

My goal here is to show different search fields depending on the user's drop down selection. I have tried this a few different ways, and nothing seems to work.

Does anyone have a simple jQuery snippet that would get the text of the Kendo UI dropdown?

My code is as follows:

  <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>
}
@(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>

Subscribe to the onSelect event and then get the selected item text. Below from kendo demo site.

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

I use Kendo MVC and my dropdownlist code is:

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

so in change func:

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

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

}

maybe can help someone :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM